在WebApplicationInitializer中读取ResourceBundle

时间:2013-10-25 22:04:44

标签: spring-mvc initializer

是否可以在WebApplicationInitializer中的Web应用初始化期间读取资源包?我在WebApplicationInitializer中初始化了一个过滤器,我想从文件中读取init-params。

我试过这种方式但是当我尝试调用setInitParameter("unprotectedUrls", unprotectedUrls);时, unprotectedUrls 为空

public class MyWebAppInitializer implements WebApplicationInitializer {

@Value("unprotectedUrls") String unprotectedUrls;


@Override
public void onStartup(ServletContext container) throws ServletException {

    System.out.println("Initializing My Web App...");
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(MyWebAppContext.class);

    container.addListener(new ContextLoaderListener(rootContext));

    javax.servlet.ServletRegistration.Dynamic servlet = container.addServlet("MyServlet", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);

    javax.servlet.FilterRegistration.Dynamic charEncodingFilterReg = container.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class);
    charEncodingFilterReg.setAsyncSupported(true);
    charEncodingFilterReg.setInitParameter("encoding", StandardCharsets.UTF_8.toString());
    charEncodingFilterReg.setInitParameter("forceEncoding", "true");
    charEncodingFilterReg.addMappingForUrlPatterns(null, false, "/*");

    javax.servlet.FilterRegistration.Dynamic authenticationFilterReg = container.addFilter("AuthenticationFilter", AuthenticationFilter.class);
    authenticationFilterReg.setAsyncSupported(true);

    container.setInitParameter("unprotectedUrls", unprotectedUrls);
    System.out.println("unprotectedUrls: " + unprotectedUrls);

    System.out.println("Initializing completed...");

}

}

2 个答案:

答案 0 :(得分:0)

当然,为什么不呢?

public class MyConfigInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        ResourceBundle bundle = ResourceBundle.getBundle("my-file.properties");
        String initParamValue = bundle.getString("key");

        Dynamic registration = servletContext.addFilter("myFilter", "some.filter.qualified.name");
        registration.setInitParameter("key", initParamValue);
    }
}

您不必使用ResourceBundle。您几乎可以在任何地方阅读这些属性。

答案 1 :(得分:0)

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        Properties properties = new Properties();
        try (BufferedInputStream input = new BufferedInputStream(WebAppInitializer.class.getClassLoader().getResourceAsStream("app.properties"))) {
            properties.load(input);
            String value = properties.getProperty("memcached.profile");
            servletContext.setInitParameter("spring.profiles.active", value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}