Springboot configuration devtools for hot deployment

Source: Internet
Author: User

Springboot configuration devtools for hot deployment

Spring provides developers with a module called Spring-boot-devtools to enable the spring boot application to support hot deployment and increase developer productivity without having to manually restart the spring boot application.

The principle of Devtools

The underlying principle is to use two ClassLoader, one ClassLoader to load the classes that will not change (third-party jar packages), and another ClassLoader to load the changed classes, called Restart ClassLoader, so that when there is code change, The original restart ClassLoader was discarded and re-created a restart ClassLoader, which resulted in a faster restart time due to fewer classes to load.

Use the following configuration to add:

<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-devtools</artifactId>        <optional>true</optional>    </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>            <configuration>                <fork>true</fork>            </configuration>        </plugin>    </plugins></build>

Description

(1) Devtools can implement the page hot deployment (that is, the page modification will take effect immediately, this can be directly configured in the Application.properties file Spring.thymeleaf.cache=false to implement),
Implement a hot deployment of the properties file by implementing a class file hot deployment that does not take effect immediately after the class file is modified.
That is, Devtools will listen for file changes under Classpath, and will immediately restart the application (taking place at save time), note: Because of its virtual machine mechanism, the restart is very fast
(2) After the configuration of the Java file is also supported after the hot start, but this method is a project restart (faster project restart), will clear the session value, that is, if a user login, the project will need to re-login after restart.

By default,/meta-inf/maven,/meta-inf/resources,/resources,/static,/templates,/public file modifications under these folders do not cause the app to restart. But it reloads (a livereload server is embedded in the Devtools, and the browser refreshes when the resource changes).

(3) If the application can be restarted if the file is to be properly classpath, use: spring.devtools.restart.additional-paths so that Devtools will include the directory in the listening range.

Configuration of the Devtools

When you configure Spring.devtools.restart.enabled=false in application.properties, the restart ClassLoader is also initialized, but the file updates are not monitored.
Call System.setproperty ("spring.devtools.restart.enabled", "false") before Sprintapplication.run, and you can completely turn off restart support and configure the content:

#热部署生效spring.devtools.restart.enabled=true#设置重启的目录#spring.devtools.restart.additional-paths=src/main/java#classpath目录下的WEB-INF文件夹内容修改不重启spring.devtools.restart.exclude=WEB-INF/**

Idea Configuration

When we modify the Java class, idea is not automatically compiled by default, and Spring-boot-devtools is monitoring the file changes under Classpath to restart the application, so you need to set the idea's automatic compilation:

(1) File-settings-compiler-build Project automatically or manually compiling with CTRL+F9

(2) CTRL + SHIFT + ALT +/, select Registry, tick Compiler Automake allow when app running

Test

    • Modify Class –> Save: App Restarts

    • Modify configuration file –> Save: app will restart

    • Modify Page –> Save: The app will not reboot but will reload and the page will refresh

      spring.thymeleaf.cache=false#classpath目录下的WEB-INF文件夹内容修改不重启spring.devtools.restart.exclude=templates/**

Reference 1

For more information, please refer to 2

Springboot configuration devtools for hot deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.