@ -16,21 +16,17 @@
package org.springframework.web ;
package org.springframework.web ;
import static org.springframework.beans.BeanUtils.instantiateClass ;
import java.lang.reflect.Modifier ;
import java.lang.reflect.Modifier ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.LinkedList ;
import java.util.List ;
import java.util.ServiceLoader ;
import java.util.ServiceLoader ;
import java.util.Set ;
import java.util.Set ;
import javax.servlet.ServletContainerInitializer ;
import javax.servlet.ServletContainerInitializer ;
import javax.servlet.ServletContext ;
import javax.servlet.ServletContext ;
import javax.servlet.ServletException ;
import javax.servlet.ServletException ;
import javax.servlet.annotation.HandlesTypes ;
import javax.servlet.annotation.HandlesTypes ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.springframework.core.annotation.AnnotationAwareOrderComparator ;
import org.springframework.core.annotation.AnnotationAwareOrderComparator ;
/ * *
/ * *
@ -94,6 +90,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
* recommendations . < p >
* recommendations . < p >
*
*
* @author Chris Beams
* @author Chris Beams
* @author Juergen Hoeller
* @since 3 . 1
* @since 3 . 1
* @see # onStartup ( Set , ServletContext )
* @see # onStartup ( Set , ServletContext )
* @see WebApplicationInitializer
* @see WebApplicationInitializer
@ -101,8 +98,6 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@HandlesTypes ( WebApplicationInitializer . class )
@HandlesTypes ( WebApplicationInitializer . class )
public class SpringServletContainerInitializer implements ServletContainerInitializer {
public class SpringServletContainerInitializer implements ServletContainerInitializer {
private static final Log logger = LogFactory . getLog ( SpringServletContainerInitializer . class ) ;
/ * *
/ * *
* Delegate the { @code ServletContext } to any { @link WebApplicationInitializer }
* Delegate the { @code ServletContext } to any { @link WebApplicationInitializer }
* implementations present on the application classpath .
* implementations present on the application classpath .
@ -129,39 +124,43 @@ public class SpringServletContainerInitializer implements ServletContainerInitia
* or any other Servlet API componentry such as filters .
* or any other Servlet API componentry such as filters .
*
*
* @param webAppInitializerClasses all implementations of
* @param webAppInitializerClasses all implementations of
* { @link WebApplicationInitializer } found on the application classpath .
* { @link WebApplicationInitializer } found on the application classpath
* @param servletContext the servlet context to be initialized
* @param servletContext the servlet context to be initialized
* @see WebApplicationInitializer # onStartup ( ServletContext )
* @see WebApplicationInitializer # onStartup ( ServletContext )
* @see AnnotationAwareOrderComparator
* @see AnnotationAwareOrderComparator
* /
* /
public void onStartup ( Set < Class < ? > > webAppInitializerClasses ,
public void onStartup ( Set < Class < ? > > webAppInitializerClasses , ServletContext servletContext )
ServletContext servletContext ) throws ServletException {
throws ServletException {
Array List< WebApplicationInitializer > initializers = new Array List< WebApplicationInitializer > ( ) ;
List < WebApplicationInitializer > initializers = new Linked List< WebApplicationInitializer > ( ) ;
if ( webAppInitializerClasses ! = null ) {
if ( webAppInitializerClasses ! = null ) {
for ( Class < ? > waiClass : webAppInitializerClasses ) {
for ( Class < ? > waiClass : webAppInitializerClasses ) {
if ( ! Modifier . isAbstract ( waiClass . getModifiers ( ) ) ) {
// Be defensive: Some servlet containers provide us with invalid classes,
// the class can be instantiated -> add it
// no matter what @HandlesTypes says...
initializers . add ( instantiateClass ( waiClass , WebApplicationInitializer . class ) ) ;
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 ( ) ) {
if ( initializers . isEmpty ( ) ) {
logger . info ( "Detected no WebApplicationInitializer types on the classpath: exiting." ) ;
servletContext . log ( "No Spring WebApplicationInitializer types detected on classpath ") ;
return ;
return ;
}
}
Collections . sort ( initializers , new AnnotationAwareOrderComparator ( ) ) ;
Collections . sort ( initializers , new AnnotationAwareOrderComparator ( ) ) ;
servletContext . log ( "Spring WebApplicationInitializers detected on classpath: " + initializers ) ;
logger . info ( "Delegating ServletContext to the following " +
"WebApplicationInitializer instances: " + initializers ) ;
for ( WebApplicationInitializer initializer : initializers ) {
for ( WebApplicationInitializer initializer : initializers ) {
initializer . onStartup ( servletContext ) ;
initializer . onStartup ( servletContext ) ;
}
}
}
}
}
}