Spring Environment Property Source Configuration

后端 未结 5 1749
感动是毒
感动是毒 2020-12-28 08:33

I\'m working on an application library with a utility class called \"Config\" which is backed by the Spring Environment object and provides strongl

5条回答
  •  佛祖请我去吃肉
    2020-12-28 08:54

    It depends on how you want to use the properties, if it is to inject the properties using ${propertyname} syntax, then yes just having PropertySourcesPlaceHolderConfigurer will work, which internally has access to the PropertySources registered in the environment.

    If you plan to use Environment directly, using say env.getProperty(), then you are right - the properties using PropertySourcesPlaceHolderConfigurer are not visible here. The only way then is to inject it using Java code, there are two ways that I know of:

    a. Using Java Config:

    @Configuration
    @PropertySource("classpath:/app.properties")
    public class SpringConfig{
    
    }
    

    b. Using a custom ApplicationContextInitializer, the way it is described here

提交回复
热议问题