是否可以在 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...");
}
}