前言
只是簡單的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表達式
有待了解,現在還不清楚具體是什么、如何使用、什么時候用。
參考:
5分鍾了解Thymeleaf的標准方言(Standard dialects)