征服Springspringbootfeign

Spring-Boot 快速入门——在tomcat容器中部署

2017-05-18  本文已影响351人  cqf_

在上一篇文章中,介绍了如何快速启动一个spring-boot的程序,但是上面的例子是通过运行main方法,调用内置的tomcat插件启动的,如果要中tomcat容器中去部署运行又该如何操作呢?

1、修改上一节demo的pom.xml文件如下

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- 移除嵌入式tomcat插件 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

2、新增一个容器启动类SpringBootStartApplication

package com.cqf.chapter2;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * 
 * @author qifu.chen
 * @version 1.0.0
 * @date May 15, 2017 10:53:30 AM
 */
public class SpringBootStartApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Application.class);
    }
}

3、部署到tomcat中并启动运行,效果如下
http://localhost:8080/chapter2/hello

chapter2-1.PNG

特别要注意的是tomcat中的servlet-api一定要是3.0以上的版本。

具体详见demo <a href="https://github.com/qifuchen/cqf-demo">chapter2</a>

你喜欢就是我最大的动力——cqf

上一篇下一篇

猜你喜欢

热点阅读