一般在生产环境都会部署在外部的web容器,比如我们公司就部署在WildFly。想这么做就要改一些配置
一 启动类的修改
public class Application extends SpringBootServletInitializer { private static final Logger appLogger = LoggerFactory.getLogger(Application.class); @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { if (appLogger.isInfoEnabled()) { appLogger.info("snrs-web start."); } SpringApplication sa = new SpringApplication(Application.class); sa.setWebEnvironment(true); sa.run(args); } }
比如继承 SpringBootServletInitializer 同时实现方法 configure
二 pom文件的修改
对于SpringBoot1版本要排除内置的tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-websocket</artifactId> </exclusion> </exclusions> </dependency>
SpringBoot2的写法是
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加servlet-api依赖--->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>//表示使用JBoss或者Tomcat自带的jar
</dependency>
同时要增加对各个环境打包的配置
<profiles> <profile> <id>dev</id> <properties> <package>jar</package> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <filters> <filter>src/main/filters/filter-dev.properties</filter> </filters> </build> </profile> <profile> <id>prd</id> <properties> <package>war</package> </properties> <build> <filters> <filter>src/main/filters/filter-prod.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>rsf.consumer.xml</exclude> <exclude>rsf.provider.xml</exclude> <exclude>application.properties</exclude> </excludes> </resource> </resources> </build> </profile> </profiles>