本文共 2401 字,大约阅读时间需要 8 分钟。
在pom.xml中将默认的jar方式改为war:
...homeworkme 0.0.1-SNAPSHOT homeworkme Demo project for Spring Boot war 1.8 ... ...
排除内置的Tomcat容器(两种方式):
1.排除spring-boot-starter-web中的Tomcat:org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
2.添加依赖:
org.springframework.boot spring-boot-starter-tomcat provided
在springBoot启动类的同级目录添加一个继承SpringBootServletInitializer
的类或者启动类继承该类,实现configure
方法。
SpringBootServletInitializer源码注释:
Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it. If you prefer to run an embedded web server then you won’t need this at all. 注意,如果您正在构建WAR文件并部署它,则需要WebApplicationInitializer。 如果你喜欢运行一个嵌入式Web服务器,那么你根本不需要这个。
package com.example.homeworkme;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer { public ServletInitializer() { System.out.println("初始化ServletInitializer"); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(HomeworkmeApplication.class);//启动类名 }}
为了防止应用上下文所导致的项目访问资源加载不到的问题,建议pom.xml文件中<build></build>
标签下添加<finalName></finalName>
标签,并设置maven打包时跳过单元测试:
war包名称 org.apache.maven.plugins maven-surefire-plugin 2.20.1 true org.springframework.boot spring-boot-maven-plugin
通过maven命令进行打包或者在IDEA顶部菜单栏目中点击Build,再选择Build Artifacts,最后点击build,在项目的target目录下就会生成对应的war包。
将生成的war包放到tomcat的webapps目录下,通过ip/项目名访问即可。
当以war包形式打包部署时,使用的是外部Tomcat,这个时候application.yml配置的端口将失效。
转载地址:http://otmg.baihongyu.com/