Spring-Boot项目部署到单独tomcat运行

1、pom.xml

<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>com.shunneng.springboot</groupId>
  <artifactId>springboot-demo2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>
<dependencies>
    <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>
            <scope>provided</scope>
    </dependency>
</dependencies>
</project>

2、启动类需要继承SpringBootServletInitializer

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 org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@SpringBootApplication
@Controller
public class HelloSpringBoot extends SpringBootServletInitializer{

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello springboot";
    }


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(HelloSpringBoot.class);
    }


    /**
     * springboot执行入口
     */
    public static void main(String[] args) {
        //SpringApplication.run(HelloSpringBoot.class, args); //不需要额外启动设置可以使用这一行代替
        SpringApplication application = new SpringApplication(HelloSpringBoot.class);
//        application.setBannerMode(Mode.OFF);//关闭banner
        application.run(args);
    }
}

猜你喜欢

转载自www.cnblogs.com/xiaofengfree/p/11420938.html