版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
ServletContainerInitializer概述
ServletContainerInitializer是Servlet 3.0规范中引入的一个接口,它的主要目的是允许开发者在Servlet容器(如Tomcat、Jetty等)启动时执行一些自定义的初始化代码。在过去,Servlet、Filter和Listener等Web组件通常需要在web.xml文件中进行静态配置,但有了ServletContainerInitializer接口,开发者就可以通过实现这个接口来动态地注册这些组件,而无需修改web.xml文件。
ServletContainerInitializer接口源码如下:
package javax.servlet;
import java.util.Set;
/**
* Interface for initializers used to programmatically register servlets, filters,
* and listeners with a ServletContainer. Implementations of this interface may
* be annotated with @HandlesTypes to declare the types of classes that they
* are interested in being notified about.
*
* <p>The container will invoke the {@link #onStartup} method of any registered
* ServletContainerInitializer implementation for which at least one class of
* interest is found during the web application's startup phase. The set of
* application classes passed to the {@link #onStartup} method will include all
* classes that match the criteria specified by the @HandlesTypes annotation,
* as well as any classes that were explicitly declared