與純Java替代xml配置讀取多個教程之後,在Initializer
類中的一個聲明,我不明白:將ContextLoaderListener添加到ServletContext的目的是什麼?
public class Initializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext applicationContext;
applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(Config.class);
// What is the purpose of the following statement?
servletContext.addListener(new ContextLoaderListener(applicationContext));
ServletRegistration.Dynamic dispatcher;
dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
我的應用程序似乎運行沒有servletContext.addListener(...)
聲明就好了。
的官方文檔裏面ServletContext
狀態,我不騙你:
/**
* TODO SERVLET3 - Add comments
* @param <T> TODO
* @param t TODO
* @throws UnsupportedOperationException If [...]
* @since Servlet 3.0
*/
public <T extends EventListener> void addListener(T t);
而且裏面JspCServletContext
的實施實際上是空的:
@Override
public <T extends EventListener> void addListener(T t) {
// NOOP
}
...那麼究竟什麼是增加的目的一個ContextLoaderListener
到ServletContext
?