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.
Note::
- 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.
32.16greet.html
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.
Test:
- 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)
Supplement:
- 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.
Reference: http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-exclude