无法在WebApplicationInitializer中加载属性文件

时间:2014-07-05 16:05:29

标签: spring spring-mvc initialization autowired properties-file

我创建了WebAppInitializer implements WebApplicationInitializer。我正在尝试使用@PropertySource读取属性文件,这在其他bean中效果很好。

问题是@Autowired Environment不起作用且env保持为空。

问:还有其他方法可以加载属性文件吗?

@PropertySource(value = {"classpath:application.properties"})
@Configuration
@Order(2)
public class WebAppInitializer implements WebApplicationInitializer {
    private static Logger logger = LoggerFactory.getLogger(WebAppInitializer.class);

    private static final String URL_REWRITE_FILTER_NAME = "urlRewrite";
    private static final String PRERENDER_FILTER_NAME = "prerender";

    private static final String URL_FILTER_MAPPING = "/*";
    private static final String URL_REWRITE_FILTER_PARAM_LOGLEVEL = "logLevel";
    private static final String URL_REWRITE_FILTER_LOGLEVEL_SLF4J = "slf4j";
    private static final String INSTANCE_NAME_CORPORATE = "<SOME INSTANCE NAME>";

    @Autowired
    private Environment env;

    @Override
    public void onStartup(ServletContext servletContext) {
        WebApplicationContext rootContext = createRootContext(servletContext);

        configureSpringMvc(servletContext, rootContext);
    }

    private WebApplicationContext createRootContext(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

        rootContext.register(SecurityConfig.class, PersistenceConfig.class, CoreConfig.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));
        servletContext.setInitParameter("defaultHtmlEscape", "true");

        return rootContext;
    }


    private void configureSpringMvc(ServletContext servletContext, WebApplicationContext rootContext) {
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(MVCConfig.class);

        mvcContext.setParent(rootContext);
        ServletRegistration.Dynamic appServlet = servletContext.addServlet(
                "webservice", new DispatcherServlet(mvcContext));
        appServlet.setLoadOnStartup(1);
        Set<String> mappingConflicts = appServlet.addMapping("/api/*");

//        Filters
        FilterRegistration.Dynamic urlReWrite = servletContext.addFilter(URL_REWRITE_FILTER_NAME, new UrlRewriteFilter());
        urlReWrite.setInitParameter(URL_REWRITE_FILTER_PARAM_LOGLEVEL, URL_REWRITE_FILTER_LOGLEVEL_SLF4J);
        EnumSet<DispatcherType> urlReWriteDispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
        urlReWrite.addMappingForUrlPatterns(urlReWriteDispatcherTypes, true, URL_FILTER_MAPPING);

/************* I NEED TO PROPERTIES FILE FOR THIS ********************      
        if (INSTANCE_NAME_CORPORATE.equals(env.getProperty("instance.name"))) { // env is null
            FilterRegistration.Dynamic prerender = servletContext.addFilter(PRERENDER_FILTER_NAME, new PreRenderSEOFilter());
            prerender.setInitParameter("prerenderToken", <SOME TOKEN>);
            EnumSet<DispatcherType> prerenderDispatcherTypes = EnumSet.of(DispatcherType.REQUEST);
            prerender.addMappingForUrlPatterns(prerenderDispatcherTypes, true, URL_FILTER_MAPPING);
        }

        if (!mappingConflicts.isEmpty()) {
            for (String s : mappingConflicts) {
                logger.error("Mapping conflict: " + s);
            }
            throw new IllegalStateException(
                    "'webservice' cannot be mapped to '/'");
        }
    }
}

0 个答案:

没有答案