使用非内置tomcat运行spring boot项目

       由于项目需要,不能使用spring boot 内置的tomcat运行spring boot项目,所以选择用普通的tomcat来运行,那么该怎么改造原有的项目依赖呢?

        新建spring boot项目,这个相信大家也都了解了,在此不多做介绍,创建好项目之后就要对项目进行改造,主要是pom文件里依赖关系的添加与修改。

    1.将打包形式改为war包


     2.屏蔽spring boot内置tomcat

  

        3.添加servlet依赖


        4.实现SpringBootServletInitializer接口

       spring-boot入口类必须实现SpringBootServletInitializer接口的configure方法才能让外部容器运行spring-boot项目

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration  
@ComponentScan  
@EnableAutoConfiguration 
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}


至此,spring boot项目就可以像普通web项目一样在普通tomcat里运行了

附上pom文件

<?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>com.jinxiong</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

<exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions> 
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId> 
    <artifactId>javax.servlet-api</artifactId> 
    <scope>provided</scope>
</dependency>
<dependency> 
    <groupId>javax.servlet.jsp</groupId> 
    <artifactId>jsp-api</artifactId> 
    <version>2.1</version> 
    <scope>provided</scope>
</dependency>
<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
<groupId>com.hynnet</groupId>
<artifactId>oracle-driver-ojdbc</artifactId>
<version>12.1.0.2</version>
</dependency> 
</dependencies>
    <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

猜你喜欢

转载自blog.csdn.net/qq_21752823/article/details/80882364