Spring WebApplicationInitializer

After Servlet 3.0, annotations are provided so that servlet configuration does not need to be done in web.xml.

 

In the original web.xml, the role of <load-on-startup>1</load-on-startup> :

1) The load-on-startup element marks whether the container loads the servlet at startup (instantiates and calls its init() method).

2) Its value must be an integer indicating the order in which the servlets should be loaded

3) When the value is 0 or greater than 0, it means that the container loads and initializes the servlet when the application starts;

4) When the value is less than 0 or not specified, it means that the container will not load until the servlet is selected.

4) The smaller the value of the positive number, the higher the priority of the servlet, and the earlier the application is loaded when it starts.

5) When the values ​​are the same, the container will choose the order to load by itself.

 

In java annotation based configuration, classes implementing WebApplicationInitializer can be loaded when the web application starts .

 

WebApplicationInitializer must be loaded through SpringServletContailnerIntializer .

public interface WebApplicationInitializer {
    void onStartup(ServletContext servletContext) throws ServletException;
}

 

SpringServletContainerInitializer

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {

    @Override
    public void onStartup(Set<Class<?>> webAppInitializerClasses,
        ServletContext servletContext) throws ServletException {

	List<WebApplicationInitializer> initializers =
            new LinkedList<WebApplicationInitializer>();

	if (webAppInitializerClasses != null) {
	    for (Class<?> waiClass : webAppInitializerClasses) {
	        if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers())
                    && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
		    try {
		        initializers.add((WebApplicationInitializer) waiClass.newInstance());
		    } catch (Throwable ex) {
			throw new ServletException("Failed to instantiate
                            WebApplicationInitializer class", ex);
		    }
		}
	    }
	}

	if (initializers.isEmpty()) {
	    servletContext.log("No Spring WebApplicationInitializer types
                detected on classpath");
	    return;
	}

	servletContext.log(initializers.size() + " Spring WebApplicationInitializers
            detected on classpath");
	AnnotationAwareOrderComparator.sort(initializers);
	for (WebApplicationInitializer initializer : initializers) {
	    initializer.onStartup(servletContext);
	}
    }
}

Note:

1. To implement the ServletContainerInitializer interface, the implemented class must be specified. @HandlesTypes plays a key role.

2. The content in META-INF/services/javax.servlet.ServletContainerInitializer in spring -web-xxx.RELEASE.jar must be a class that implements ServletContainerInitializer.

 

javax.servlet.ServletContainerInitializer:

org.springframework.web.SpringServletContainerInitializer

 

ServletContainerIntializer:

public interface ServletContainerInitializer {
    void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326352040&siteId=291194637