前言

只是简单的properties配置学习,修改部分“约定”改为自定义“配置”。真正使用和遇到问题是在细看。

一、主要

核心只是demo中的: @PropertySource(value = "classpath:/config/custom.properties", ignoreResourceNotFound = true)

二、demo

// application 注解
@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(value = "classpath:/config/custom.properties", ignoreResourceNotFound = true)
// Controller 注解
@Controller
public class PropertiesController {
@Value("${CONSTANT_PASSWORD}")
private String password; @Autowired
private ConfigBean configBean; @RequestMapping("/custom")
public String custom(@RequestParam(value="name", required=false, defaultValue="${CONSTANT_USER}") String name
, Model model) {
model.addAttribute("name", name);
model.addAttribute("password", password);
System.out.println(configBean);
return "custom";
} public static void main(String[] args) {
SpringApplication.run(PropertiesController.class, args);
}
}

config/custom.properties

## 常量配置
CONSTANT_USER=vergiyln
CONSTANT_PASSWORD=中文123 ## thymeleaf 配置
spring.thymeleaf.prefix=classpath:/templates/properties/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false

config/ConfigBean.properties  注入bean配置(日期暂时不知道怎么注入)

vergilyn.map[blog]=http://www.cnblogs.com/VergiLyn/
vergilyn.map[name]=VergiLyn
vergilyn.map[remark]=备注,中文23333 vergilyn.list[0]=Carpenters
vergilyn.list[1]=Celine Dion
vergilyn.list[2]=Bon Jovi
vergilyn.list[3]=Taylor Swift vergilyn.str=string
vergilyn.num=124
vergilyn.date=2017-01-14 23:55:19
vergilyn.isSuccess=false

properties对应的JavaBean

@Configuration
// prefix = value,看源码
@ConfigurationProperties(prefix = "vergilyn")
@PropertySource("classpath:config/ConfigBean.properties")
@Component
public class ConfigBean implements Serializable{
private String str; private Integer num; // 注意:要是setIsSuccess(),不能是setSuccess() private boolean isSuccess;
// 日期不知道怎么注入// private Date date; private Map<String, String> map; private List<String> list; public String getStr() {
return str;
} public void setStr(String str) {
this.str = str;
} public Integer getNum() {
return num;
} public void setNum(Integer num) {
this.num = num;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Boolean isSuccess() {
return isSuccess;
} public void setIsSuccess(Boolean success) {
isSuccess = success;
}
}

custom.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>测试properties</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'user: ' + ${name}" />
<p th:text="'password: ' + ${password}" />
</body>
</html>

测试结果:

二、扩展

1、idea中注入properties中文乱码。

起初,我以为需要*.properties或者*.xml或者别的什么配置中要配置编码,但发现并不是。

参考:Springboot 之 解决IDEA读取properties配置文件的中文乱码问题

2、注入属性类型。

如demo,基本上string、int是可以的,不用多余的配置。但测试date并不知道怎么注入。

特别需要注意的是isSuccess;生成的set是setSuccess(),但properties中确实isSuccess导致注入失败。

3、@ConfigurationProperties中可以设置前缀

Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
@AliasFor("prefix")
String value() default ""; @AliasFor("value")
String prefix() default ""; boolean ignoreInvalidFields() default false; boolean ignoreNestedProperties() default false; boolean ignoreUnknownFields() default true; boolean exceptionIfInvalid() default true; /** @deprecated */
@Deprecated
String[] locations() default {}; /** @deprecated */
@Deprecated
boolean merge() default true;
}

如源码,可以看出prefix与value是一样的。

4、thymeleaf 表达式、方言

${...}:变量表达式;

*{...}:选择变量表达式;

#{...}:消息表达式;

@{...}:链接url表达式

有待了解,现在还不清楚具体是什么、如何使用、什么时候用。

参考:

thymeleaf 基本语法

5分钟了解Thymeleaf的标准方言(Standard dialects)

附录

玩转spring boot——properties配置

最新文章

  1. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
  2. 基础学习day11--多线程一线程的创建,运行,同步和锁
  3. C#调用NPOI组件导出Excel表格
  4. 【JavaScript】AJAX总结(异步JavaScript和XML)
  5. 八位彻底改变App Store的iOS开发者
  6. linux ARP攻击处理
  7. 字符串解析成easyui-tree的格式
  8. JavaScript中获取当前项目的绝对路径
  9. make module失败的原因cc1: error: unrecognized command line option “-m64
  10. MySQL常用函数及日期
  11. 翻译连载 | 附录 A:Transducing(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
  12. 让Android Support V4中的SwipeRefreshLayout支持上拉载入很多其它
  13. BAT面试技巧
  14. jmetter的http请求设置
  15. ztree模糊筛选展开选中节点
  16. UVa 10905 - Children&#39;s Game 排序,题目没有说输入是int 难度: 0
  17. Django URLConf 进阶
  18. 监控Tomcat
  19. JavaScript——双向链表实现
  20. jquery ready方法实现原理

热门文章

  1. 3. JavaScript 数据类型
  2. Strusts2--课程笔记5
  3. Redis简介三
  4. HDU 5804 Price List
  5. PAT (Top Level) Practise 1005 Programming Pattern (35)
  6. 《TCP/IP详解》读书笔记
  7. 其他应用和技巧-用Json格式来保存数据
  8. document.domain的修改问题
  9. hdu_2110_Crisis of HDU(母函数)
  10. away 3d的一些问题