时间:2020-10-31 17:06:02 | 栏目:JAVA代码 | 点击:次
springboot微服务内置了tomcat,在工程目录下执行:mvn clean package,可以将项目打成jar,通过java -jar jar包名.jar启动项目。
有哪些场景需要将springboot打成war包去部署呢?
1.一个tomcat管理多个项目
2.springboot整合jsp等
解决方法:
1.<packaging>jar</packaging>中的jar改成war
2.引入依赖:
<dependency> <groupid>org.springframework.boot</groupid> spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> spring-boot-starter-tomcat</artifactid> </exclusion> </exclusions> </dependency> <!--servlet依赖--> <dependency> <groupid>javax.servlet</groupid> javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.apache.tomcat</groupid> tomcat-servlet-api</artifactid> <version>8.0.36</version> <scope>provided</scope> </dependency>
3.启动类继承SpringBootServletInitializer,重写configure方法:
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } </code></code> }
4.修改tomcat配置文件context.xml
修改标签:
<context> 至<context xmlblockexternal="false">,就可以打包部署了 </context></context>
总结