17.Spring Boot加载指定YML文件

您所在的位置:网站首页 springboot怎么部署到服务器 17.Spring Boot加载指定YML文件

17.Spring Boot加载指定YML文件

2023-06-18 01:55| 来源: 网络整理| 查看: 265

目录

1. Spring Boot基本介绍及Eclipse插件安装(STS)

2. Spring Boot基本使用(Thymeleaf模板、国际化)

3. Spring Boot使用Thymeleaf模板引擎

4. Thymeleaf标签详细

5. Spring Boot自动配置

6. Spring Boot配置文件(yml、properties)

7. Spring Boot集成常用配置参数

8. Spring Boot常用注解及JPA注解

9. Spring Boot事务

10. Spring Boot热部署

11. Spring Boot中使用JSP

12. Spirng Boot自定义错误页面

13. Spring Boot防止表单重复提交实现方式

14. Spring Boot监听器、过滤器、拦截器

15. Spring Boot单元测试(Service、Mock、Feign、Http Rest API)

16. Spring Boot多线程基本使用

17. Spring Boot加载指定YML文件

SpringBoot加载指定YML文件 目录SpringBoot加载指定YML文件(1)YamlPropertiesFactoryBean:将yaml文件加载为Properties(2)YamlPropertySourceLoader:将yaml文件加载为Map

SpringBoot加载指定YML文件

Spring Boot默认支持properties和yml配置文件的读取,前者格式简单,但是只支持键值对。如果需要表达列表,最好使用YAML格式。

Spring Boot支持自动加载约定名称的配置文件,仅支持指定路径下指定名称的配置文件;例如application.yml。当自定义指定路径加载配置文件时,properties文件使用@PropertySource注解即可,但该注解并不支持加载yaml

老版本的SpringBoot可以通过@ConfigurationProperties的方式指定location;1.0.4之后该属性就取消了

@ConfigurationProperties(locations={"classpath:myconfig.yml"}) (1)YamlPropertiesFactoryBean:将yaml文件加载为Properties @Configuration public class YamlPropertyConfig { /** * 将yaml文件转为properties并设置到属性源 * @return */ @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yamlProperties = new YamlPropertiesFactoryBean(); yamlProperties.setResources(new ClassPathResource("generator.yml")); propertySourcesPlaceholderConfigurer.setProperties(yamlProperties.getObject()); return propertySourcesPlaceholderConfigurer; } } (2)YamlPropertySourceLoader:将yaml文件加载为Map @Configuration public class YamlPropertyConfig { @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer2() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); MutablePropertySources sources = new MutablePropertySources(); try { List


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3