spring boot 打war包在外部tomcat没法运行

1.处理spring boot项目打的war包在外部tomcat没办法运行起来并访问的问题java

第一步:spring boot项目的启动类继承自SpringBootServletInitializerweb

由于在外部容器部署的话,就不能依赖于Application的main函数了,而是要以相似于web.xml文件配置的方式来启动spring应用上下文,所以须要在启动类继承SpringBootServletInitializer并实现configure方法,这个类的做用与在web.xml中配置负责初始化spring应用上下文的监听器做用相似。代码以下图:spring

第二步:修改pom文件,让spring boot内嵌的tomcat在运行时不起做用浏览器

有两种办法,任选一种:tomcat

<dependency>
    <!--声明spring boot内嵌tomcat的做用范围,在运行时不起做用-->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

或者ide

<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>

最后启动外部的tomcat,在浏览器中访问时,须要添加项目名字,也就是war解压后文件夹的名字函数

相关文章
相关标签/搜索