springboot应用在tomcat中运行

1.将打包方式改成war,因为如果是java -jar xx.jar方式运行,一定是jar包

 <packaging>war</packaging>

2.添加tomcat的依赖,但是注意作用域(scope)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

3.配置JavaBean,就是要继承SpringBootInitializer类,重写configure方法

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootApplication.class);
    }

}

4.用tomcat8以上,经验:用tomcat7及以下时,会报如下 错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path

猜你喜欢

转载自www.cnblogs.com/Lyn4ever/p/11495048.html