In my web application I am creating a CustomWebApplicationInitializer which is extending from AbstractAnnotationConfigDispatcherServletInitializer. I need to add another property source for the application. I am doing so in the onStartup method as below by setting the InitParameter in servletContext.
public class MvcWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
private static final String[] SERVLET_MAPPINGS = new String[] {"/"};
private static final String SESSION_COOKIE_PATH = "/";
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return SERVLET_MAPPINGS;
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.setInitParameter("contextInitializerClasses", "com.test.myproject.MyTestPropertySourceInitializer");
}
}
MyTestPropertySourceInitializer implements ApplicationContextInitializer and checks if the value of a certain property is set to true.
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();
Boolean testPropEnabled = (Boolean) env.getProperty("testProperty.enabled", Boolean.class);
I have set that property value to true. But in the logs value is false and it does not process as expected. So I think that by the time the initialize method is called, the property file is not loaded in applicationContext. Please advise how I can get this working. Thanks in advance.
try, by force load the properties file
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();
env.getPropertySources().addFirst(new ResourcePropertySource("classpath:application.properties"))
Boolean testPropEnabled = (Boolean) env.getProperty("testProperty.enabled", Boolean.class);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.