zoukankan      html  css  js  c++  java
  • springboot 加载自定义yml文件

    Springboot加载自定义yml文件配置的方法

    1. ConfigurationProperties注解的locations属性在1.5.X以后没有了,不能指定locations来加载yml文件

      image-20200724155412600

    2. PropertySource注解不支持yml文件加载,详细见官方文档:

    image-20200724155336975

    1. Spring Framework有两个类加载YAML文件,YamlPropertiesFactoryBean和YamlMapFactoryBean

    image-20200724155457932

    1. 可以通过PropertySourcePlaceholderConfigurer来加载yml文件,暴露yml文件到spring environment
    /**
     * @author WGR
     * @create 2020/7/24 -- 15:31
     */
    @Configuration
    public class SpringBootConfigura {
        // 加载YML格式自定义配置文件
        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new ClassPathResource("config.yml"));//File引入
            configurer.setProperties(yaml.getObject());
            return configurer;
        }
    }
    
    

    配置文件:

    my:
      servers:
        - dev.example.com
        - another.example.com
        - ${random.value}
    
    1. 测试
    @Controller
    public class SpringBootTest {
    
        @Autowired
        Config config;
    
        @GetMapping("/testConfig")
        @ResponseBody
        public String testConfig(){
            return config.getServers().toString();
        }
    }
    

    image-20200724155645621

  • 相关阅读:
    angular笔记_6
    angular笔记_5(全选/反选)
    angular笔记_4(函数)
    angular笔记_3
    angular笔记_2
    常用Sql语句
    IIS服务器环境下某路径下所有PHP接口无法运行报500.19错误
    #前端#文字、图像等元素居中方式之
    nginx如何设置禁止访问文件或文件夹
    git克隆和上传项目
  • 原文地址:https://www.cnblogs.com/dalianpai/p/13372414.html
Copyright © 2011-2022 走看看