美文网首页
springboot-配置通配符获取properties文件

springboot-配置通配符获取properties文件

作者: xhrg | 来源:发表于2018-11-27 13:12 被阅读0次

    springboot获取配置文件默认是这样获取

    @Configuration
    @PropertySource("file:/data/xxx/dubbo.properties")
    public class Beans {
        @Value("${dubbo.application.name}")
         private String type;
    

    如果要配置多个则需要@PropertySource("file:/data/xxx/dubbo.properties","xxx1","xxx2")

    如果要通配符的方式去配置,可以实现PropertySourcesPlaceholderConfigurer的一个bean。

    改后的代码如下:

    @Configuration
    public class Beans {
    
        @Value("${dubbo.application.name}")
        private String type;
        
        @Bean
        public DataSource testBean() {
            DataSource dataSource=new MysqlDataSource();
            return dataSource;
        }
        
        @Bean  // 一定一定一定要注意,这里的PropertySourcesPlaceholderConfigurer获取方法是静态的,如果不是静态的则会失败。
        public static PropertySourcesPlaceholderConfigurer getPropertyPlaceholderConfigurer() 
                  throws IOException { 
              PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); 
              ppc.setLocations(new PathMatchingResourcePatternResolver().getResources("file:/data/xxx/xxx/*.properties")); 
              return ppc; 
        } 
        
    }
    

    相关文章

      网友评论

          本文标题:springboot-配置通配符获取properties文件

          本文链接:https://www.haomeiwen.com/subject/ntdgqqtx.html