之前我们讲了SpringBoot的jar包方式启动,这里进行war包部署讲解,与传统的部署方式大致一样,传统mvc项目maven热部署方式请看我之前的博客
1.war包部署首先在pom文件中将打包方式改成war
<!-- 打包方式 -->
<packaging>war</packaging>
2.加上打包的项目名称(没什么卵用):
<!-- 打包的项目名称 -->
<finalName>springbootdemo</finalName>
3.修改启动类,继承SpringBootServletInitializer类并重写configure()方法:
package com.qzsun.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import javax.swing.*;
@SpringBootApplication
public class SpringbootdemoApplication extends SpringBootServletInitializer {
//打成war包需要继承SpringBootServletInitializer并重写configure方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootdemoApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
4.加一个controller进行测试:
@Controller
public class IndexController {
@RequestMapping("/index")
@ResponseBody
public Map<String, Object> index(){
System.out.println("热部署1");
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
return map;
}
}
5.使用maven 命令打一个war包(mvn install),然后将war包丢到tomcat的wepapps目录中,在bin目录下启动startup.bat
6.访问接口成功(这里我修改了tomcat的端口号为8081),