SpringBoot系列:SpringBoot打包成war,并在tomcat下运行

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

war

3、在pom中将spring-boot-starter-tomcat的scope设置成provided

如果打包成war时,不会打包tomcat相关的jar

如果打包成jar时,会打包tomcat相关jar


		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		

4、配置maven-war-plugin插件

注意:failOnMissingWebXml要设置成false,因为springboot是没有web.xml,否则mvn会出错

outputDirectory设置成tomcat下webapps目录下,可以自动复制到webapps目录下


			
				org.apache.maven.plugins
				maven-war-plugin
				
					
					false
					myweb
					
						
							${start-class}
							true
						
					
					true
					true
					true
					
					${outputDirectory}
				
			

5、完整配置



	4.0.0

	com.example
	demo
	0.0.1-SNAPSHOT
	war

	demo

	
		UTF-8
		UTF-8
		1.7
		1.5.13.RELEASE
		D:/Program Files/Tomcat/apache-tomcat-7.0.70/webapps
	
	
	
		
			
			
				org.springframework.boot
				spring-boot-dependencies
				${springboot.version}
				pom
				import
			
		
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-devtools
			runtime
		
		
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

	

	
		
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					${java.version}
					${java.version}
				
			

			
			
				org.apache.maven.plugins
				maven-war-plugin
				
					
					false
					myweb
					
						
							${start-class}
							true
						
					
					true
					true
					true
					
					${outputDirectory}
				
			
			
			
			
			
			
			
				org.apache.maven.plugins
				maven-resources-plugin
				
					UTF-8
					
						@
					
					false
				
			

			
				org.apache.maven.plugins
				maven-surefire-plugin
				
					true
				
			
		

	


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


你可能感兴趣的:(SpringBoot,tomcat,打包)