spring boot after modify the code without having to restart settings to achieve hot deployment at development

(Automatic restart the application after modifying the file save development time) Spring Boot achieve hot deployment at development time (spring-boot-devtools)

What is hot deployment

We all know that in the project development process, often change the page data or modify the data structure, in order to show changes in effect, often need to restart the application to see the effect of changing, in fact, re-compiled the new Class file, this file records and code the like corresponding to various kinds of information, and the virtual file will be Class ClassLoader loading machine.

The hot deployment took advantage of this feature, if it listens to the Class file changes, it will create a new ClaassLoader loading the file, through a series of process, the final result will be presented in front of us.

Class loading mechanism

Compiled Java classes may be compiled into the code stored in the Class file byte code, the Class file stores a variety of information, eventually to be loaded into the virtual machine operational use.

Class loading mechanism (excerpt from "in-depth understanding of the Java Virtual Machine")

The virtual machine data that describes the classes loaded from Class file into memory, and verify the data, and converts parsing initialization, forming Java type can be used as a virtual machine.

Spring Boot achieve hot deployment

Spring Boot achieve thermal deployed as follows:

Spring Loaded

In this way in the form of Maven plug-ins to load, so when you start using mvn spring-boot by Maven command: run start, but will start by Application.run ineffective way, because when you start the application through, has bypasses Maven plug-in mechanism.

POM integrated approach:

Copy the code
<build>
    <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build>
Copy the code

spring-boot-devtools

In this way no matter how start the application, you can modify the file after reaching restart the application.

POM integrated approach:

<!-- 热部署模块 -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency>

集成注意:

1、如果发现没有热部署效果,则需要检查IDE配置中有没有打开自动编译。

2、如果使用Thymeleaf模板引擎,需要把模板默认缓存设置为false

#禁止thymeleaf缓存(建议:开发环境设置为false,生成环境设置为true)
spring.thymeleaf.cache=false

3、针对devtools的可以指定目录或者排除目录来进行热部署

# Add the directory files need to restart 
spring.devtools.restart.additional-Paths = src / main / the Java 
# exclude that directory files do not need to restart 
spring.devtools.restart.exclude = static / **, public / **

4, by default, / META-INF / maven, / META-INF / resources, / resources, / static, / templates, / public folders in the file does not modify the application to restart, but will reload (devtools embedded a LiveReload Server, when a change in resource, refresh your browser)

5, disposed in the application.properties spring.devtools.restart.enabled = false, this time will restart initialization class loader, but does not monitor file updates. Called before SprintApplication.run System.setProperty ( "spring.devtools.restart.enabled", "false"); can be completely closed to restart support.

Guess you like

Origin www.cnblogs.com/qianzf/p/11356215.html