[英]Spring web Application Context
In my web application I am creating a CustomWebApplicationInitializer which is extending from AbstractAnnotationConfigDispatcherServletInitializer. 在我的Web应用程序中,我正在创建一个CustomWebApplicationInitializer,该类是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.
我在下面的onStartup方法中通过在ServletContext中设置InitParameter来执行此操作。
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. MyTestPropertySourceInitializer实现ApplicationContextInitializer并检查某个属性的值是否设置为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. 我已将该属性值设置为true。 But in the logs value is false and it does not process as expected.
但是在日志中,该值为false,并且无法按预期进行处理。 So I think that by the time the initialize method is called, the property file is not loaded in applicationContext.
因此,我认为在调用initialize方法时,属性文件尚未加载到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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.