J2Cache 一級緩存數據
https://blog.csdn.net/luckyrocks/article/details/79248016
根據以上博客,學習到的文件 引入問題
1. ConfigurationProperties注解的locations屬性在1.5.X以后沒有了,不能指定locations來加載yml文件
2. PropertySource注解只支持yml文件加載,詳細見官方文檔: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings
3. Spring Framework有兩個類加載YAML文件,YamlPropertiesFactoryBean和YamlMapFactoryBean
4. 可以通過PropertySourcePlaceholderConfigurer來加載yml文件,暴露yml文件到spring environment
// 加載YML格式自定義配置文件
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new FileSystemResource("config.yml"));//File引入
// yaml.setResources(new ClassPathResource("youryml.yml"));//class引入
configurer.setProperties(yaml.getObject());
return configurer;
}
5. 可以直接通過@Value引入屬性值注入Bean
@Component
@ConfigurationProperties(prefix = "prefix")
public class ConfigYML1 {
// 以下屬性可以直接獲取
private String name;
private List<Map<String, String>> list = new ArrayList<>();
@Value("${your.username}")
private String username;
}
6. config.yml
prefix:
name:
list:
- name: tech
key: 123
source: beijing
- name: skill
key: 987
source: shanghai
---
your:
username: test
---
list2:
name: qwer
url: http://blog.csdn.net/luckyrocks
7. Question:
在yml文件中,‘---’表示分隔符,表示多個yml,當Component在ConfigurationProperties注解的屬性中prefix設置值后,list2無法直接加載為map,即如果你聲明一個list2的map是沒有值的,只能通過list2.name以字符串的形式獲取到
8. Solutions:
yml文件以‘---’分隔的屬性無法在寫有前綴的情況下獲取
a. 寫到另外一個yml文件中,重新加載
b. 將list2后面的屬性放到'prefix'前綴的后面,也可以獲取
始終找不到滿意的解決方法,你是怎么解決的?請告訴我
參考資料:
1. http://blog.csdn.net/tyrant_800/article/details/78780312
2. http://m.mamicode.com/info-detail-2006405.html
3. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings
