Accessing multiple property files with @PropertyResource in Spring

后端 未结 3 1520
盖世英雄少女心
盖世英雄少女心 2020-12-08 15:17

Using the new @PropertySource annotation in Spring 3.1, how can you access multiple property files with Environment?

Currently I have:

@         


        
相关标签:
3条回答
  • 2020-12-08 16:04

    Multiple Properties can be accessed in Spring by either,

    • @PropertySource( {"name1", "name2"} )
    • @PropertySorces( { @PropertySource("name1"), @PropertySource("name2") } )

    Example of @PropertySource,

    @PropertySource({
            "classpath:hibernateCustom.properties",
            "classpath:hikari.properties"
    })
    

    Example of @PropertySources,

    @PropertySources({
            @PropertySource("classpath:hibernateCustom.properties"),
            @PropertySource("classpath:hikari.properties")
    })
    

    After you specify properties path you can access them through Environment instance as you usually did

    NOTE: Only these simply did not work for me though

    I was getting compilation error as I was using property values to configure my app context. I tried all that I found through web but they did not work for me !

    Until I configured Spring context as below,

    applicationContext.setServletContext(servletContext);
    applicationContext.refresh();
    

    Example

    public class SpringWebAppInitializer implements WebApplicationInitializer{
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            // Create the 'root' Spring application context
            AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
            // register config class i.e. where i used @PropertySource
            applicationContext.register(AppContextConfig.class);
            // below 2 are added to make @PropertySources/ multi properties file to work
            applicationContext.setServletContext(servletContext);
            applicationContext.refresh();
    
            // other config
        }
    }
    

    For your information, I am using Spring 4.3

    0 讨论(0)
  • 2020-12-08 16:11

    there are two different approaches: the first one is to use the PropertyPlaceHolder in your applicationContext.xml: beans-factory-placeholderconfigurer

    <context:property-placeholder location="classpath*:META-INF/spring/properties/*.properties"/>
    

    the namespace to add is xmlns:context="http://www.springframework.org/schema/context"

    If you want a direct access of a key to a String variable in your controller, use:

    @Value("${some.key}")
    private String valueOfThatKey;
    

    The second approach is to use the util:properties in you applicationContext.xml:

    <util:properties id="fileA" location="classpath:META-INF/properties/a.properties"/>
    <util:properties id="fileB" location="classpath:META-INF/properties/b.properties"/>
    

    using the namesapce xmlns:util="http://www.springframework.org/schema/util" schemaLocations: http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd

    Then in your Controller:

    @Resource(name="fileA")
    private Properties propertyA;
    
    @Resource(name="fileB")
    private Properties propertyB;
    

    If you want a value from the files, just use the method getProperty(String key)

    0 讨论(0)
  • 2020-12-08 16:13

    If you can migrate to Spring 4.x the problem has been solved with the new @PropertySources annotation:

    @PropertySources({
            @PropertySource("/file1.properties"),
            @PropertySource("/file2.properties")
    })
    
    0 讨论(0)
提交回复
热议问题