spring boot配置tomcat部署

x33g5p2x  于2021-11-27 转载在 Spring  
字(3.2k)|赞(0)|评价(0)|浏览(546)

spring boot本身默认为jar包运行,可以改为war包,然后运行在tomcat里

具体修改的步骤如下:

1.在pom.xml文件里添加需要的依赖:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--以下是在tomcat运行比spring boot多引的包-->
<dependency>           
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

以上两个依赖还需要引入下边一个spring boot的父依赖。

<!-- Spring Boot 启动父依赖 -->
<parent>
 	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.1.RELEASE</version>
</parent>

为了访问到jsp页面还需要加jstl依赖

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

为了部署到tomcat里,那packing 标签必须要改成war

<packaging>war</packaging>
2.最后修改启动类:
package microservice.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * 即继承SpringBootServletInitializer类,并重写它的 configure方法即可
 */
@SpringBootApplication
public class CreateModelApplication extends SpringBootServletInitializer{
    public static void main(String[] args){
        SpringApplication.run(CreateModelApplication.class,args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CreateModelApplication.class);
    }
}

注意了,这样配置,并不是说只能用tomcat来启动,也是可以直接main方法启动的,瞬间高大上了不是?

3.然后在项目的src/main/下创建一个新的文件夹webapp,我们放进两个jsp文件和js用来测试,配置到这里,就可以启动tomcat查看了,配置情况如下图:

4.启动tomcat,访问index.jsp

5.如果你还想让将来打包的结构规范一些,可以在pom.xml里加上如下配置:
<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>application.properties</include>
                <include>dubbo-consumer.xml</include>
                <include>logback-spring.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>${basedir}/src/main/webapp</directory>
            <!--这里是将webapp下的文件都打包复制到下边目录下 -->
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/**</include>
            </includes>
        </resource>
        <resource>
         <!--下边的这个resource,这个其实在这里没有用,可以删掉-->  <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>**/**</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.5.1.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
    </plugins>
</build>

相关文章