Using the new @PropertySource annotation in Spring 3.1, how can you access multiple property files with Environment?
Currently I have:
@
Multiple Properties can be accessed in Spring by either,
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
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