Programmatic access to properties created by property-placeholder

后端 未结 9 2398
长情又很酷
长情又很酷 2020-11-29 20:21

I\'m reading properties file using context:property-placeholder. How can I access them programatically (@Value doesn\'t work - I d

9条回答
  •  孤城傲影
    2020-11-29 21:16

    We use the following approach to access properties for our applications

    
    
    

    Then you have the luxury of just autowiring properties into beans using a qualifier.

    @Component
    public class PropertyAccessBean {
    
        private Properties properties;
    
        @Autowired
        @Qualifier("appProperties")
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public void doSomething() {
            String property = properties.getProperty("code.version");
        }
    
    }
    

    If you have more complex properties you can still use ignore-resource-not-found and ignore-unresolvable. We use this approach to externalise some of our application settings.

     
     
    

提交回复
热议问题