1

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

我以这种方式尝试过,但是当我尝试调用时unprotectedUrls为空setInitParameter("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...");

}

}

4

2 回答 2

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. 您几乎可以从任何地方读取属性。

于 2013-10-26T04:05:00.153 回答
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();
        }
    }
}
于 2015-08-12T14:58:54.863 回答