赞
踩
1、实现继承SpringBootServletInitializer类
- package com.example.demo;
-
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.web.support.SpringBootServletInitializer;
-
- public class ServletInitializer extends SpringBootServletInitializer {
-
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(DemoApplication.class);
- }
-
- }
- package com.example.demo;
-
- import javax.servlet.Filter;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
- import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.filter.CharacterEncodingFilter;
-
- @SpringBootApplication
- public class DemoApplication {
-
- // 用于处理编码问题
- @Bean
- public Filter characterEncodingFilter() {
- CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
- characterEncodingFilter.setEncoding("UTF-8");
- characterEncodingFilter.setForceEncoding(true);
- return characterEncodingFilter;
- }
-
- /**
- * mvn打包后,需要指定内嵌tomcat的服务,否则会“找不到EmbeddedServletContainerFactory”报错
- * 也可以使用jetty服务
- * @return
- */
- @Bean
- public EmbeddedServletContainerFactory servletContainer() {
- TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
- return factory;
- }
-
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
- }

2、将SpringBoot打包方式改成war
<packaging>war</packaging>
3、在pom中将spring-boot-starter-tomcat的scope设置成provided
如果打包成war时,不会打包tomcat相关的jar
如果打包成jar时,会打包tomcat相关jar
- <!-- scope=provided,当打包成war时不会将tomcat.jar打包进去;当直接eclipse执行DemoApplication时需要
- 一定要配置<scope>provided</scope>
- -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
4、配置maven-war-plugin插件
注意:failOnMissingWebXml要设置成false,因为springboot是没有web.xml,否则mvn会出错
outputDirectory设置成tomcat下webapps目录下,可以自动复制到webapps目录下
- <!-- war发布打包,并war包直接输出到tomcat下 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <configuration>
- <!-- 设置成false,否则检查web.xml是否存在。因为springboot是没有web.xml的 -->
- <failOnMissingWebXml>false</failOnMissingWebXml>
- <warName>myweb</warName>
- <archive>
- <manifest>
- <mainClass>${start-class}</mainClass>
- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
- </manifest>
- </archive>
- <skipTests>true</skipTests>
- <skip>true</skip><!-- 跳过测试 -->
- <testFailureIgnore>true</testFailureIgnore>
- <!-- 将war包直接输出到tomcat下webapps目录下 -->
- <outputDirectory>${outputDirectory}</outputDirectory>
- </configuration>
- </plugin>

5、完整配置
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.example</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
-
- <name>demo</name>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.7</java.version>
- <springboot.version>1.5.13.RELEASE</springboot.version>
- <outputDirectory>D:/Program Files/Tomcat/apache-tomcat-7.0.70/webapps</outputDirectory>
- </properties>
-
- <dependencyManagement>
- <dependencies>
- <!--springboot相关依赖 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${springboot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
-
- <!-- scope=provided,当打包成war时不会将tomcat.jar打包进去;当直接eclipse执行DemoApplication时需要
- 一定要配置<scope>provided</scope>
- -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
-
- <build>
- <plugins>
- <!-- 编译插件,以${java.version}编译 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- </configuration>
- </plugin>
-
- <!-- war发布打包,并war包直接输出到tomcat下 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <configuration>
- <!-- 设置成false,否则检查web.xml是否存在。因为springboot是没有web.xml的 -->
- <failOnMissingWebXml>false</failOnMissingWebXml>
- <warName>myweb</warName>
- <archive>
- <manifest>
- <mainClass>${start-class}</mainClass>
- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
- </manifest>
- </archive>
- <skipTests>true</skipTests>
- <skip>true</skip><!-- 跳过测试 -->
- <testFailureIgnore>true</testFailureIgnore>
- <!-- 将war包直接输出到tomcat下webapps目录下 -->
- <outputDirectory>${outputDirectory}</outputDirectory>
- </configuration>
- </plugin>
-
- <!-- jar发布打包,并jar包直接输出到指定目录下 -->
- <!-- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>1.5.9.RELEASE</version>
- <configuration>
- 指定该Main Class为全局的唯一入口
- <mainClass>com.example.demo.DemoApplication</mainClass>
- <layout>ZIP</layout>
- <outputDirectory>${outputDirectory}</outputDirectory>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>可以把依赖的包都打包到生成的Jar包中
- </goals>
- </execution>
- </executions>
- </plugin> -->
-
- <!-- 将resources目录下文件打包进去 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <configuration>
- <encoding>UTF-8</encoding>
- <delimiters>
- <delimiter>@</delimiter>
- </delimiters>
- <useDefaultDelimiters>false</useDefaultDelimiters>
- </configuration>
- </plugin>
-
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <skipTests>true</skipTests>
- </configuration>
- </plugin>
- </plugins>
-
- </build>
-
-
- </project>

6、运行tomcat并访问
通过http://127.0.0.1:8080/myweb/index访问
如果不需要contextPath, 可以将war打包到webapps下的ROOT目录, 则http://127.0.0.1:8080/index访问
注意:application.properties中的端口和contextPath配置不再生效了,而是使用tomcat的方式
- server.port=8081
- server.context-path=/mydemo
代码已上传到github:https://github.com/suncht/sun-test/tree/master/springboot.tomcat.test
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。