传统的web应用都需要配置web.xml,web容器读取web.xml获取服务配置和servlet、filter、listener等配置,实例化添加进容器提供服务。学习《springboot编程思想》自动装配章节中,一个简单注解配置就可以实现无web.xml启动web应用,而且也不是springboot启动方式。
一、实现方式:
- 继承AbstractAnnotationConfigDispatcherServletInitializer,替代web.xml
public class SpringWebMVCServlatInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
// 读取配置类,从而扫描controller
protected Class<?>[] getServletConfigClasses() {
return of(SpringWebMVCConfiguration.class);
}
// 配置DispatcherServlet路径
protected String[] getServletMappings() {
return of("/*");
}
private static <T> T[] of(T... values) {
return values;
}
}
- SpringWebMVCConfiguration.class
// @EnableWebMvc打开springmvc配置,@ComponentScan扫描controller包,加载controller
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "controller")
public class SpringWebMVCConfiguration {
}
2.测试controller
@Controller
public class HelloController {
@PostConstruct
public void init() {
System.out.println("init HelloController......");
}
@RequestMapping
@ResponseBody
public String hello() {
return "hello world";
}
}
4.配置文件,配置jar方式启动
需要注意:添加的tomcat maven插件是将应用和tomcat打包到一个jar,配置main class启动tomcat进而加载应用,可以解压打包的jar查看详情。
<properties>
<spring_version>5.2.3.RELEASE</spring_version>
<servlet-version>LATEST</servlet-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>
maven-war-plugin
</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<!-- 最终打包成可执行的jar包 -->
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<!-- ServletContext 路径 -->
<path>/</path>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
参考作者示例:https://github.com/mercyblitz/thinking-in-spring-boot-samples/tree/master/spring-framework-samples
5.启动并测试:
java -jar ./target/springboot_auto-1.0-SNAPSHOT-war-exec.jar
二、原理分析:
-
AbstractAnnotationConfigDispatcherServletInitializer类
AbstractAnnotationConfigDispatcherServletInitializer继承关系图如下:
实现了接口WebApplicationInitializer,springmvc应用启动时会调用其onStartup方法,该方法创建WebApplicationContext上下文类,并根据配置创建servletcontext。
2.那么onStartup什么时候调用的呢?
WebApplicationInitializer.onStartup 是通过SpringServletContainerInitializer来调用的,SpringServletContainerInitializer源码如下:
@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
public SpringServletContainerInitializer() {
}
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList();
Iterator var4;
if (webAppInitializerClasses != null) {
var4 = webAppInitializerClasses.iterator();
while(var4.hasNext()) {
Class<?> waiClass = (Class)var4.next();
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer)ReflectionUtils.accessibleConstructor(waiClass, new Class[0]).newInstance());
} catch (Throwable var7) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", var7);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
} else {
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
var4 = initializers.iterator();
while(var4.hasNext()) {
WebApplicationInitializer initializer = (WebApplicationInitializer)var4.next();
initializer.onStartup(servletContext);
}
}
}
}
SpringServletContainerInitializer 的onStartup 方法会处理@HandlesTypes注解的类,通过反射实例化继承该类的子类,并调用其onStartup 方法。
-
SpringServletContainerInitializer又是哪里调用的呢?
SpringServletContainerInitializer 实现了 ServletContainerInitializer 接口,实现了ServletContainerInitializer接口的类在META-INF/services路径下添加配置便可以被容器加载,如spring-web包下:
内容如下:
org.springframework.web.SpringServletContainerInitializer
这个机制称作SPI,是Servlet 3.0引进的,由容器读取META-INF/services实现。
可以看出该配置是容器提供的代码配置支持,并且可以与web.xml配置方式可以同时生效。