首页 > 代码库 > springboot项目怎么部署到外部tomcat
springboot项目怎么部署到外部tomcat
spring-boot项目中,默认提供内嵌的tomcat,所以打包直接生成jar包,用Java -jar命令就可以启动。
但是也有一定的需求,会使用外部tomcat来部署项目。下面来看:
1.新建项目boot-tomcat-test
2.pom依赖:(添加spring-boot-starter-tomcat依赖,打包方式为war)
<?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>boot-tomcat</groupId> <artifactId>com.boot.tomcat</artifactId> <version>1.0-SNAPSHOT</version> <!--父依赖包--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies> <!-- tomcat 打war包 --> <packaging>war</packaging> </project>
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 */ @SpringBootApplication public 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 */ @RestController public class IndexController { @RequestMapping("/") public String index(){ return "hello boot"; } }
5.配置项目到tomcat,然后启动tomcat
访问:http://localhost:8080/
ok了!
springboot项目怎么部署到外部tomcat
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。