ピュアJavaでXML設定を置き換えるには、いくつかのチュートリアルを読んだ後、私は理解していないInitializer
クラス内の1つの文があります: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
?