1. pom 中改动
1.1. jar改为war
<packaging>war</packaging>
1.2.starter-web排除内置tomcat
增加exclusions标签
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
1.3. 编译所需包
编译时候需要依赖一些servlet包,所以我们可以引用servlet包,也可以用tomcat包,只要是comple级别就可以
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
2.java部分改动
开启servletinitialer的启动方式,扩展springbootservletinitializer实现configure方法
先看下启动类:
package com.tdt.audit;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ImportResource({"classpath:/spring-ehcache.xml"})
public class VgiAuditApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(VgiAuditApplication.class, args);
// ApplicationContextProviderHelper.setApplicationContext(context);
}
}
方式一. 可以是直接在启动类上实现
package com.tdt.audit;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ImportResource({"classpath:/spring-ehcache.xml"})
public class VgiAuditApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(VgiAuditApplication.class, args);
// ApplicationContextProviderHelper.setApplicationContext(context);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(VgiAuditApplication.class);
}
}
方式二,可以新建一个类实现
新建类ServletInitializer
package com.tdt.audit;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(VgiAuditApplication.class);
}
}