springboot应用war包部署tomcat

springboot的应用打包默认是打成jar包,并且如果是web应用的话,默认使用内置的tomcat充当servlet容器,但毕竟内置的tomcat有时候并不满足我们的需求,如有时候我们想集群或者其他一些特性优化配置,因此我们需要把springboot的jar应用打包成war包,并能够在外部tomcat中运行。
    很多人会疑问,你直接打成war包并部署到tomcat的webapp下不就行了么?No,springboot的如果在类路径下有tomcat相关类文件,就会以内置tomcat启动的方式,经过你把war包扔到外置的tomcat的webapp文件下启动springBoot应用也无事于补。
    要把springboot应用转至外部tomcat的操作主要有以下三点:
1、把pom.xml文件中打包结果由jar改成war,如下:
  1. <modelVersion>4.0.0 </modelVersion>
  2. <groupId>spring-boot-panminlan-mybatis-test </groupId>
  3. <artifactId>mybatis-test </artifactId>
  4. <packaging>war </packaging>
  5. <version>0.0.1-SNAPSHOT </version>

2、添加maven的war打包插件如下:并且给war包起一个名字,tomcat部署后的访问路径会需要,如:http:localhost:8080/myweb/****

  
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <configuration>
  5. <warSourceExcludes>src/main/resources /**</warSourceExcludes>
  6. <warName>myweb</warName>
  7. </configuration>
  8. </plugin>

3、排除org.springframework.boot依赖中的tomcat内置容器,这是很重要的一步


  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>

4、添加对servlet API的依赖
  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. </dependency>

5、继承 SpringBootServletInitializer ,并覆盖它的  configure 方法,如下图代码,为什么需要提供这样一个SpringBootServletInitializer子类并覆盖它的config方法呢,我们看下该类原代码的注释:
/**Note that a WebApplicationInitializer is only needed if you are building a war file and
 * deploying it. If you prefer to run an embedded container then you won't need this at
 * all.
如果我们构建的是wai包并部署到外部tomcat则需要使用它,如果使用内置servlet容器则不需要,外置tomcat环境的配置需要这个类的configure方法来指定初始化资源。
  1. @SpringBootApplication
  2. // mapper 接口类扫描包配置
  3. public class Application extends SpringBootServletInitializer{
  4. public static void main(String[] args) throws IOException {
  5. // 程序启动入口
  6. Properties properties = new Properties();
  7. InputStream in = Application.class.getClassLoader().getResourceAsStream( "app.properties");
  8. properties.load(in);
  9. SpringApplication app = new SpringApplication(Application.class);
  10. app.setDefaultProperties(properties);
  11. app.run(args);
  12. /*EmbeddedServletContainerAutoConfiguration*/
  13. }
  14. @Override
  15. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  16. // TODO Auto-generated method stub
  17. builder.sources( this.getClass());
  18. return super.configure(builder);
  19. }
经过以上配置,我们把构建好的war包拷到tomcat的webapp下,启动tomcat就可以访问啦
springboot的应用打包默认是打成jar包,并且如果是web应用的话,默认使用内置的tomcat充当servlet容器,但毕竟内置的tomcat有时候并不满足我们的需求,如有时候我们想集群或者其他一些特性优化配置,因此我们需要把springboot的jar应用打包成war包,并能够在外部tomcat中运行。
    很多人会疑问,你直接打成war包并部署到tomcat的webapp下不就行了么?No,springboot的如果在类路径下有tomcat相关类文件,就会以内置tomcat启动的方式,经过你把war包扔到外置的tomcat的webapp文件下启动springBoot应用也无事于补。
    要把springboot应用转至外部tomcat的操作主要有以下三点:
1、把pom.xml文件中打包结果由jar改成war,如下:
  1. <modelVersion>4.0.0 </modelVersion>
  2. <groupId>spring-boot-panminlan-mybatis-test </groupId>
  3. <artifactId>mybatis-test </artifactId>
  4. <packaging>war </packaging>
  5. <version>0.0.1-SNAPSHOT </version>

2、添加maven的war打包插件如下:并且给war包起一个名字,tomcat部署后的访问路径会需要,如:http:localhost:8080/myweb/****

  
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <configuration>
  5. <warSourceExcludes>src/main/resources /**</warSourceExcludes>
  6. <warName>myweb</warName>
  7. </configuration>
  8. </plugin>

3、排除org.springframework.boot依赖中的tomcat内置容器,这是很重要的一步


  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>

4、添加对servlet API的依赖
  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. </dependency>

5、继承 SpringBootServletInitializer ,并覆盖它的  configure 方法,如下图代码,为什么需要提供这样一个SpringBootServletInitializer子类并覆盖它的config方法呢,我们看下该类原代码的注释:
/**Note that a WebApplicationInitializer is only needed if you are building a war file and
 * deploying it. If you prefer to run an embedded container then you won't need this at
 * all.
如果我们构建的是wai包并部署到外部tomcat则需要使用它,如果使用内置servlet容器则不需要,外置tomcat环境的配置需要这个类的configure方法来指定初始化资源。
  1. @SpringBootApplication
  2. // mapper 接口类扫描包配置
  3. public class Application extends SpringBootServletInitializer{
  4. public static void main(String[] args) throws IOException {
  5. // 程序启动入口
  6. Properties properties = new Properties();
  7. InputStream in = Application.class.getClassLoader().getResourceAsStream( "app.properties");
  8. properties.load(in);
  9. SpringApplication app = new SpringApplication(Application.class);
  10. app.setDefaultProperties(properties);
  11. app.run(args);
  12. /*EmbeddedServletContainerAutoConfiguration*/
  13. }
  14. @Override
  15. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  16. // TODO Auto-generated method stub
  17. builder.sources( this.getClass());
  18. return super.configure(builder);
  19. }
经过以上配置,我们把构建好的war包拷到tomcat的webapp下,启动tomcat就可以访问啦

猜你喜欢

转载自blog.csdn.net/adsadadaddadasda/article/details/81015188
今日推荐