美文网首页
SpringBoot build war and tomcat

SpringBoot build war and tomcat

作者: mysmy1099 | 来源:发表于2019-04-04 17:54 被阅读0次

我们都知道Springboot 都会自带tomcat容器,如果我们想独立出去到自己tomcat进行部署该如何操作呢?
接下来就来设置一下吧
第一步(先自己去官网下载一个tomcat并且启动起来先)
第二步(配置项目pom 文件配置)

<packaging>war</packaging>
<!--<packaging>jar</packaging>--> change to war

#排除掉默认的tomcat
<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>

<!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,
运行等周期。相当于compile,但是打包阶段做了exclude操作-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

#build 我通常这样设置
<build>
    <finalName>fight-pc</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    </build>

第三步(修改启动类配置 继承 SpringBootServletInitializer 实现configure方法即可)

@SpringBootApplication
@MapperScan({"com.dl.pc.mapper","com.dl.pc.mapper.ex"})
@ComponentScan(basePackages={"com.dl.pc","com.youfei.tools"})
@EnableScheduling
public class SportsPcApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ReadYml.init();
        SpringApplication.run(SportsPcApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SportsPcApplication.class);
    }
}

大功告成自己去maven clear package试一下吧
最后把生成的war包放到tomcat webapps目录下

注意本地启动的时候需要把注释掉

<scope>provided</scope>

tomcat 占用内存还挺高的,启动了一下就没了7 800M,看看以后怎么优化


image.png

相关文章

网友评论

      本文标题:SpringBoot build war and tomcat

      本文链接:https://www.haomeiwen.com/subject/iauqiqtx.html