欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot war 部署

程序员文章站 2022-06-21 09:14:54
...
1.spring-boot的下载安装 
 
2.打开spring-boot的工程,修改 pom.xml 文件
   将

<packaging>jar</packaging> 

改成

<packaging>war</packaging>

添加新的依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

在正式发布时最好写成下面的去除springboot的tomcat的依赖再添加真正的tomcat依赖
<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>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
在<build>节点里填写finalName为你的项目访问名称

<build>
<outputDirectory>target/classes</outputDirectory>
<finalName>项目访问名称</finalName>

.....

</build>

3.修改原来spring-boot的启动类继承SpringBootServletInitializer类并且重写SpringApplicationBuilder函数

@Configuration//配置控制
@EnableAutoConfiguration//启用自动配置
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan({ "com.test" })
public class Application extends SpringBootServletInitializer {

private static Class<Application> applicationClass = Application.class;

/*
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
*/

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}


@Bean
public Filter initializeHttpPutHandler() {
return new HttpPutFormContentFilter();
}

@Bean
public CharacterEncodingFilter initializeCharacterEncodingFilter() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
return filter;
}
}

至此可以将项目打包成war发布到tomcat,或者在eclipse用tomcat插件调试了.

 

 

http://blog.163.com/gibby_l/blog/static/8300316120162110245135/