Technical Introduction
- Devtools: is a boot of a hot deployment tool, when we modify the files under the Classpath (including class files, properties files, pages, etc.), the application will be restarted (due to its use of the two-class loader mechanism, this start will be very fast, If this boot is found to be slow, you can choose to use Jrebel)
- two-class loader mechanism : Boot uses two ClassLoader to implement the restart (restart) Mechanism:base class loader (BC)+Restart class loader (RC).
- BC: Used to load a jar that does not change (eg. third-party dependent jars)
- RC: Used to load the jar we are developing (eg. the class we wrote ourselves throughout the project). When the application restarts, the original RC is discarded, re-new to the RC to load the modified things, and BC does not need to move a bit. This is the reason why the devtools restarts quickly.
- thymeleaf:boot recommended template engine, here is a brief introduction, to introduce Devtools to the hot deployment of the page.
Project Structure :
1, Pom.xml
<!--Thymeleaf - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-thymeleaf</Artifactid> </Dependency> <!--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 - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-devtools</Artifactid> <Optional>True</Optional><!--optional=true, dependencies are not passed, the project relies on Devtools, and projects that depend on myboot projects, if you want to use Devtools, need to reintroduce - </Dependency>
Note : If you only use Thymeleaf, you only need to introduce thymeleaf, and if you want to use Devtools, simply introduce Devtools.
Note :
- Optional=true in Maven means that dependencies are not passed. The devtools referenced here are not delivered to projects that depend on the Myboot project.
- Just adding devtools is not working in our eclipse, and we need to make some changes to the previously added Spring-boot-maven-plugin, as follows:
<!--The jar used to make the application run directly (the jar is used in a production environment) it is worth noting that if you do not refer to Spring-boot-starter-parent as the parent and use the second method described above, you should make the appropriate changes here. -<plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> <Configuration> <Fork>True</Fork> <!--without this configuration, Ken's devtools will not work, i.e. the app will not restart - </Configuration></plugin>
The fork:true is added
2, Thymeleafcontroller
PackageCom.xxx.firstboot.web;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportIo.swagger.annotations.Api;Importio.swagger.annotations.ApiOperation; @Api ("Test thymeleaf and Devtools") @Controller @requestmapping ("/thymeleaf") Public classThymeleafcontroller {@ApiOperation ("First Thymeleaf program") @RequestMapping (value= "/greeting", method =requestmethod.get) PublicString Greeting (@RequestParam (name = "Name", required =false, DefaultValue = "World"String name, model model) {Model.addattribute ("XName", name); return"Greet"; }}
Description : Model can be used as an entry parameter, in the code, the attribute is "Key-value" in the form of the model, and finally directly return the string.
3, greet.html
<!DOCTYPE HTML><HTMLxmlns:th= "http://www.thymeleaf.org"><Head> <title>First Thymeleaf Program</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /></Head><Body> <PTh:text= "' Hello, ' + ${xname} + '! '" /> <Div>1234567890!!! Xx</Div></Body></HTML>
Note :
- Src/main/resources/templates: Page Store Directory
- Src/main/resources/static: Mode static file (CSS, JS, etc.)
The above directories are not the same as those developed in SSM and will be placed under Src/main/webapp in SSM
Test:
- Modify Class-Save: App Restarts
- Modify configuration file-Save: App Restarts
- Modify Page-Save: The app will not reboot, but will reload and the page will refresh (the principle is to set Spring.thymeleaf.cache to false)
Add:
- By default,/meta-inf/maven,/meta-inf/resources,/resources,/static,/templates,/public files under these folders are modified The app will not be restarted , but will reload (devtools embedded with a livereload server, and the browser refreshes when the resource changes).
- If you want to change the default settings, you can set the directory that does not restart: spring.devtools.restart.exclude=static/**,public/**, so that only the file changes in these two directories will not cause restart operation.
- If you want to add additional exclude directories based on the remaining default settings: Spring.devtools.restart.additional-exclude
- If you want the application to restart when the file that is not classpath is changed, use: Spring.devtools.restart.additional-paths so that Devtools will include the directory in the listening scope.
"17th Chapter" Springboot + Devtools (Hot Deployment)