Spring Security(Third Edition)
上QQ阅读APP看书,第一时间看更新

The ContextLoaderListener class

The first step of updating the web.xml file is to remove it and replace it with javax.servlet.ServletContainerInitializer, which is the preferred approach to Servlet 3.0+ initialization. Spring MVC provides the o.s.w.WebApplicationInitializer interface, which leverages this mechanism. In Spring MVC, the preferred approach is to extend o.s.w.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer. The WebApplicationInitializer class is polymorphically o.s.w.context.AbstractContextLoaderInitializer and uses the abstract createRootApplicationContext() method to create a root ApplicationContext, then delegates it to ContextLoaderListener, which is registered in the ServletContext instance, as shown in the following code snippet:

    //src/main/java/c/p/s/web/configuration/WebAppInitializer

public class WebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { JavaConfig.class, SecurityConfig.class,
DataSourceConfig.class };
}
...
}

The updated configuration will now load SecurityConfig.class from the classpath of the WAR file.