spring-boot项目中,默认提供内嵌的tomcat,所以打包直接生成jar包,用Java -jar命令就可以启动。
但是也有一定的需求,会使用外部tomcat来部署项目。下面来看:
1.新建项目boot-tomcat-test
2.pom依赖:(添加spring-boot-starter-tomcat依赖,打包方式为war)
4.0.0 boot-tomcat com.boot.tomcat 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.3.2.RELEASE UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat war
3.启动App继承SpringBootServletInitializer类,并重写configure方法
package boottest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.web.SpringBootServletInitializer;/** * App类描述: * 继承SpringBootServletInitializer,可以用tomcat部署 * @author yangzhenlong * @since 2017/5/3 */@SpringBootApplicationpublic class App extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(App.class); } public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); }}
4.Controller:
package boottest;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * IndexController类描述: * * @author yangzhenlong * @since 2017/5/3 */@RestControllerpublic class IndexController { @RequestMapping("/") public String index(){ return "hello boot"; }}
5.配置项目到tomcat,然后启动tomcat
访问:http://localhost:8080/
ok了!