一步步学习java后台-原理篇(一) ContextLoader
2020-05-03 本文已影响0人
milawoai
ContextLoaderListener 是Spring框架提供的对sevlet的监听器。SpringMVC工程总会在Web.xml中添加这一段:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
当我们打开其源码来看下ContextLoaderListener的定义:
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
...
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
public void contextInitialized(ServletContextEvent event) {
this.initWebApplicationContext(event.getServletContext());
}
public void contextDestroyed(ServletContextEvent event) {
this.closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
ServletContextListener
ServletContextListener 是 servlet 提供的对后台进程的监听接口。继承这个接口,
public void contextInitialized(ServletContextEvent event) ** 对应服务器启动;public void contextDestroyed(ServletContextEvent event) ** 对应服务器关闭
观察源码我们就可以知道:
- 服务器启动 -> initWebApplicationContext
- 服务器关闭 -> closeWebApplicationContext
参考文献: