SpringBoot攻略九、使用外部tomcat启动、热部署

it2022-05-05  125

1、pom.xml修改如下:

<packaging>war</packaging> <!-- spring boot支持内嵌tomcat和外部tomcat,provided表示本地开发测试编译期间可用(tomcat-embed-jasper内嵌支持jsp),依然可以使用main方法启动,但不会打到war包中 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 热部署,无需重启内嵌tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>

application-dev.properties

#热部署 #开启热部署 spring.devtools.restart.enabled=true #重启目录 spring.devtools.restart.additionalPaths=src/main/java #修改静态内容不会重启 spring.devtools.restart.exclude=WEB-INF/** #页面不加载缓存,修改即时生效 spring.freemarker.cache=false

2、启动类Application继承SpringBootServletInitializer 覆盖方法:

@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); }

我们一起来了解一下其中的原理

根据Servlet3.0规范,web应用启动时会实例化每个jar包中的ServletContainerInitializer提供者(SPI)。实现了ServletContainerInitializer的jar包会在META-INF/services目录下维护一个文件javax.servlet.ServletContainerInitializer,其内容指向ServletContainerInitializer的实现类。如spring-web中:

看看SpringServletContainerInitializer做了哪些事情: @HandlesTypes注解表示SpringServletContainerInitializer能处理的类型。

SpringBootServletInitializer实现了WebApplicationInitializer


最新回复(0)