Classes that implement the Webapplicationinitializer interface can be loaded when the Web application is started.
Then think of a question: Why does the Onstartup method automatically execute when the Webapplicationinitializer interface is implemented?
Let us briefly analyze the implementation of its principle, the following first paste the webapplicationinitializer interface Related code:
Servletcontainerinitializer interface for servlet :
PackageJavax.servlet;ImportJava.util.Set; Public InterfaceServletcontainerinitializer {/*** receives notification during startup of a Web application of the classes * within the Web application that Matched the criteria defined via the * {@linkjavax.servlet.annotation.HandlesTypes} annotation. * * @paramc The (possibly null) set of classes that met the specified * criteria *@paramCTX The ServletContext of the Web application in which the * classes were discovered * * /c0>@throwsservletexception If An error occurs*/ voidOnstartup (set<class<?>> C, ServletContext CTX)throwsservletexception;}
The Springservletcontainerinitializer class (starting with this class is the code for spring):
PackageOrg.springframework.web;ImportJava.lang.reflect.Modifier;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.ServiceLoader;ImportJava.util.Set;ImportJavax.servlet.ServletContainerInitializer;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;Importjavax.servlet.annotation.HandlesTypes;Importorg.springframework.core.annotation.AnnotationAwareOrderComparator; @HandlesTypes (Webapplicationinitializer .class) Public classSpringservletcontainerinitializerImplementsServletcontainerinitializer {/*** Delegate the {@codeServletContext} to any {@linkWebapplicationinitializer} * Implementations present on the application classpath. * * <p>because This class declares @{@codehandlestypes (Webapplicationinitializer.class)}, * Servlet 3.0+ containers would automatically scan the classpath f or implementations * of Spring ' s {@codeWebapplicationinitializer} interface and provide the set of all * Such types to the {@codewebappinitializerclasses} parameter of this method. * * <p>if No {@codeWebapplicationinitializer} implementations is found on the * Classpath and this method is effectively a no-op. An info-level log message would be is * issued notifying the user that the {@codeServletcontainerinitializer} has indeed * been invoked and that no {@codeWebapplicationinitializer} implementations were * found. * * <p>assuming that one or more {@codeWebapplicationinitializer} types is detected, * They'll be instantiated (and <em>sorted</em> if th E @{@link* Org.springframework.core.annotation.Order @Order} annotation is present or * the {@linkorg.springframework.core.Ordered Ordered} interface has been * implemented). then the {@linkWebapplicationinitializer#onstartup (ServletContext)} * method is invoked on each instance, delegating the { /c4>@codeServletContext} Such * that's instance may register and configure servlets such as Spring ' s * {@codeDispatcherservlet}, listeners such as Spring ' s {@codeContextloaderlistener}, * or any other Servlet API componentry such as filters. * * @paramwebappinitializerclasses All implementations of * {@linkWebapplicationinitializer} found on the application Classpath *@paramServletContext The servlet context to be initialized *@seeWebapplicationinitializer#onstartup (servletcontext) *@seeAnnotationawareordercomparator*/@Override Public voidOnstartup (set<class<?>>webappinitializerclasses, ServletContext ServletContext)throwsservletexception {List<WebApplicationInitializer> initializers =NewLinkedlist<webapplicationinitializer>(); if(Webappinitializerclasses! =NULL) { for(class<?>waiclass:webappinitializerclasses) { //BES defensive:some servlet containers provide us with invalid classes,//no matter what @HandlesTypes says ... if(!waiclass.isinterface () &&! Modifier.isabstract (Waiclass.getmodifiers ()) &&Webapplicationinitializer.class. IsAssignableFrom (Waiclass)) { Try{initializers.add (Webapplicationinitializer) waiclass.newinstance ()); } Catch(Throwable ex) {Throw NewServletexception ("Failed to instantiate Webapplicationinitializer class", ex); } } } } if(Initializers.isempty ()) {ServletContext.log ("No Spring Webapplicationinitializer types detected on Classpath"); return; } annotationawareordercomparator.sort (initializers); ServletContext.log ("Spring Webapplicationinitializers detected on classpath:" +initializers); for(Webapplicationinitializer initializer:initializers) {initializer.onstartup (ServletContext); } }}
Webapplicationinitializer interface (the interface discussed in this article):
PackageOrg.springframework.web;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException Public InterfaceWebapplicationinitializer {/*** Configure the given {@linkServletContext} with any servlets, filters, listeners * context-params and attributes necessary for initializing This Web application. See * Examples {@linkplainWebapplicationinitializer above}. * @paramServletContext the {@codeServletContext} to initialize *@throwsServletexception If any call against the given {@codeServletContext} * throws a {@codeServletexception}*/ voidOnstartup (ServletContext ServletContext)throwsservletexception;}
Javax.servlet.ServletContainerInitializerFile (in Path:spring-framework/spring-web/src/main/resources/Meta-inf/ Services/Next )
The file name is "javax.servlet.ServletContainerInitializer", the content of which is only the following line
Org.springframework.web.SpringServletContainerInitializer
At this point, the relevant code/files are given above.
Then see the Springservletcontainerinitializer class, which implements the servlet servletcontainerinitializer interface with " @HandlesTypes (Webapplicationinitializer. Class) "annotation, which indicates servletcontainerinitializer interface can use.
and must be in spring-web-X. Under the X.x.release-sources.jar file, there is a file named "Javax.servlet.ServletContainerInitializer" under the Meta_inf/services path, The content in the file is the same as the fully qualified name of the Servletcontainerintializer implementation class (refer to the code posted above). this way, when the Web app starts, the program will find the implementation class that Webapplicationinitializer this interface and call the Onstartup method.
If the place is wrong, please correct me, thank you.
Reference:
http://blog.csdn.net/sunhuwh/article/details/25441217
http://blog.csdn.net/sunhuwh/article/details/25504061
Spring Source: Https://github.com/spring-projects/spring-framework
Original articles, reproduced please specify: http://www.cnblogs.com/langtianya/p/5393633.html
Webapplicationinitializer (Spring 3.x.x or later)