SpringBoot (7)-hot loading and packaging deployment

Hot deployment will not be used in a production environment, but it is still helpful to the efficiency of programmer development. The so-called hot deployment is to implement new deployments without stopping applications. When we first started learning the SSM framework, we manually configured Tomcat ourselves, and we can also directly set up hot loading. So how should it be set up in Spring Boot?


First we need to add a dependency, as follows:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

spring-boot-devtools is a module for developers. Its deep principle is to use two ClassLoaders, one Classloader to load classes that will not change (such as third-party Jar packages), and the other ClassLoader to load classes that will change , Called restart ClassLoader,
so that when there is a code change, the original restart ClassLoader is discarded, and a restart ClassLoader is recreated. Since there are fewer classes to load, a faster restart time is achieved.


It should be noted here that if we have integrated Thymeleaf templates or FreeMarker templates in the project, then we need to make relevant settings in the configuration file

spring.thymeleaf.cache=false
spring.freemarker.cache=false



So far, if we are using Eclipse, then it is OK, but if we are using IDEA, then we need to make some related settings, because IDEA is not saved and modified, that is to say, not in IDEA The code will be recompiled because of Ctrl + S.


In IDEA, we also need to add a compile plugin in the pom file, so that when the code changes, it will also compile
Insert picture description here

In this way, we can manually complete the hot loading in IDEA, why is it manual hot loading, because here we modify the code, press the shortcut key Ctrl + F9, manually build the project, or only modify a single class file, press Ctrl + Shift + F9, recompile this type of file, you can trigger the restart of the service.


Maybe you will think that this is too much trouble, so can you automatically hot load? Of course it is possible, but we also need to make some settings in IDEA, as follows:
Insert picture description here


Then press the shortcut key Ctrl + Shift + Alt + /, select 1.Registry ..., check compiler.automake.allow.when.app.running.
Insert picture description here
Insert picture description here




Finally, let ’s take a brief look at the problem of project packaging and deployment. If we want to package a project for deployment, if we want to reach a jar package, first we need to add relevant configuration in the pom file, and then package it through Maven.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>



But if you don't want to use the built-in tomcat and want to deploy to other Tomcat servers, then you need to use the war package to deploy. Then we first need to modify the way of packaging in the pom file
Insert picture description here


Then we better remove the built-in tomcat support to avoid conflict with external Tomcat

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<!--
    scope值的作用范围:
        compile:默认值,适用于所有阶段,并且会随着项目直接发布
        provided:编译和测试阶段有效,并且在运行时由服务器提供
        runtime:运行时使用,对测试和运行有效,如jdbc
        test:只在测试时使用,在编译和运行时不起作用,不发项目不起作用
        system:不依赖maven仓库解析,需要提供依赖的显示路径,对项目的移植来说很不方便
-->

Finally, we also need to modify the startup class of SpringBoot to inherit from
org.springframework.boot.web.servlet.support.SpringBootServletInitializer and rewrite the configure method
Insert picture description here

286 original articles published · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/newbie0107/article/details/105421660