Chapter 2 springboot + devtools (hot deployment) and springbootdevtools

Source: Internet
Author: User

Chapter 2 springboot + devtools (hot deployment) and springbootdevtools

Technical Introduction

  • Devtools: It is a hot Deployment Tool for boot. When we modify files under classpath (including class files, attribute files, pages, and so on, the application will be restarted (because of its two-class loader mechanism, this startup will be very fast. If you find this startup is slow, you can choose to use jrebel)
    • Double-Class Loader Mechanism: Boot uses two class loaders to implement the restart mechanism:Base Class Loader (bc)+Restart Class Loader (rc).
      • Bc: used to load jar that will not be changed (eg. Third-party dependent jar)
      • Rc: used to load the jar we are developing (for example, the class we wrote in the entire project ). After the application is restarted, the original rc is discarded and a new rc is added to load the modified items, but the bc does not need to be updated. This is why devtools restarts quickly.
  • Thymeleaf:The recommended template engine for boot is briefly introduced here to introduce the hot deployment of devtools on pages.

Project Structure:

1. pom. xml

1 <! -- Thymeleaf --> 2 <dependency> 3 <groupId> org. springframework. boot </groupId> 4 <artifactId> spring-boot-starter-thymeleaf </artifactId> 5 </dependency> 6 <! -- 7 devtools can implement hot page deployment (that is, the page will take effect immediately after modification, which can be directly deployed in application. configure spring in the properties file. thymeleaf. cache = false), 8 implements hot deployment of class files (class files do not take effect immediately after modification), and implements hot deployment of attribute files. 9. devtools listens to file changes under classpath and immediately restarts the application (at the time of storage). Note: because of the virtual machine mechanism, 10 --> 11 <dependency> 12 <groupId> org. springframework. boot </groupId> 13 <artifactId> spring-boot-devtools </artifactId> 14 <optional> true </optional> <! -- Optional = true: the dependency is not passed. This project depends on devtools. If you want to use devtools for projects dependent on myboot, you need to re-introduce --> 15 </dependency>View Code

Description: If you only use thymeleaf, you only need to introduce thymeleaf. If you need devtools, you only need to introduce devtools.


  • In maven, optional = true indicates that the dependency will not be passed. That is, the referenced devtools will not be passed to the project dependent on the myboot project.
  • Adding devtools only does not work in our eclipse. At this time, you also need to make some modifications to the spring-boot-maven-plugin added earlier, as shown below: 1 <! -- Used to compress an application into a jar that can be run directly (this jar is used in the production environment). It is worth noting that if spring-boot-starter-parent is not referenced for parent, 2. The second method is used, and corresponding changes are required here --> 3 <plugin> 4 <groupId> org. springframework. boot </groupId> 5 <artifactId> spring-boot-maven-plugin </artifactId> 6 <configuration> 7 <fork> true </fork> <! -- If this configuration is not available, devtools won't work, that is, the application won't restart --> 8 </configuration> 9 </plugin>View Code

    AddedFork: true


2. ThymeleafController

1 package com. xxx. firstboot. web; 2 3 import org. springframework. stereotype. controller; 4 import org. springframework. ui. model; 5 import org. springframework. web. bind. annotation. requestMapping; 6 import org. springframework. web. bind. annotation. requestMethod; 7 import org. springframework. web. bind. annotation. requestParam; 8 9 import io. swagger. annotations. api; 10 import io. swagger. annotations. apiOperation; 11 12 @ Api ("test Thymeleaf and devtools") 13 @ Controller14 @ RequestMapping ("/thymeleaf ") 15 public class ThymeleafController {16 17 @ ApiOperation ("the first thymeleaf program") 18 @ RequestMapping (value = "/greeting", method = RequestMethod. GET) 19 public String greeting (@ RequestParam (name = "name", required = false, defaultValue = "world") String name, 20 Model model) {21 model. addAttribute ("xname", name); 22 return "greet"; 23} 24 25}View Code

Description: Model can be used as an input parameter. In the code, the attribute is saved to the model in the form of "key-value", and a string is directly returned.



1 <! Doctype html> 2 Note::

  • Src/main/resources/templates: page storage directory
  • Src/main/resources/static: static files (css, js, etc)

The preceding directories are different from those developed in ssm. ssm is stored in src/main/webapp.



  • Modify class --> Save: The application restarts.
  • Modify the configuration file --> Save: The application restarts.
  • Modify the page --> Save: The application will restart and the page will be refreshed (the principle is to set spring. thymeleaf. cache to false)


  • By default,/META-INF/maven,/META-INF/resources,/resources,/Static,/Templates,/PublicThe application will not be restarted, but will be reloaded.(Devtools is embedded with a LiveReload server, which is refreshed by the browser when resources change ).
    • If you want to change the default settings, you can set the directory without restart: spring. devtools. restart. exclude = static/**, public/**. In this way, only the file modifications under these two directories will not cause restart operations.
    • If you want to add another exclusion directory based on the default settings: spring. devtools. restart. additional-exclude
  • If you want to restart the application when the files in a non-classpath file change, use spring. devtools. restart. additional-paths to include the directory in the listening scope.



Related Article

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