赵计刚
每天进步一点点

技术介绍

  • devtools:是boot的一个热部署工具,当我们修改了classpath下的文件(包括类文件、属性文件、页面等)时,会重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,如果发现这个启动比较慢,可以选择使用jrebel)
    • 双类加载器机制:boot使用了两个类加载器来实现重启(restart)机制:base类加载器(简称bc)+restart类加载器(简称rc)
      • bc:用于加载不会改变的jar(eg.第三方依赖的jar)
      • rc:用于加载我们正在开发的jar(eg.整个项目里我们自己编写的类)。当应用重启后,原先的rc被丢掉、重新new一个rc来加载这些修改过的东西,而bc却不需要动一下。这就是devtools重启速度快的原因。
  • thymeleaf:boot推荐的模板引擎,这里做简要的介绍,用来介绍devtools对页面的热部署。

项目结构

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可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现),
 8             实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。
 9             即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的
10          -->
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-devtools</artifactId>
14             <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
15         </dependency>
View Code

说明:如果仅仅使用thymeleaf,只需要引入thymeleaf;如果需要使用devtools,只需要引入devtools。

注意

  • maven中的optional=true表示依赖不会传递。即此处引用的devtools不会传递到依赖myboot项目的项目中。
  • 仅仅加入devtools在我们的eclipse中还不起作用,这时候还需要对之前添加的spring-boot-maven-plugin做一些修改,如下:
    1             <!-- 用于将应用打成可直接运行的jar(该jar就是用于生产环境中的jar) 值得注意的是,如果没有引用spring-boot-starter-parent做parent, 
    2                 且采用了上述的第二种方式,这里也要做出相应的改动 -->
    3             <plugin>
    4                 <groupId>org.springframework.boot</groupId>
    5                 <artifactId>spring-boot-maven-plugin</artifactId>
    6                 <configuration>
    7                     <fork>true</fork><!-- 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
    8                 </configuration>
    9             </plugin>
    View Code

    即添加了fork: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("测试Thymeleaf和devtools")
13 @Controller
14 @RequestMapping("/thymeleaf")
15 public class ThymeleafController {
16 
17     @ApiOperation("第一个thymeleaf程序")
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

说明:Model可以作为一个入参,在代码中,将属性以"key-value"的形式存入model,最后直接返回字符串即可。

 

3、greet.html

 1 <!DOCTYPE HTML>
 2 <html xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <title>第一个thymeleaf程序</title>
 5     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 6 </head>
 7 <body>
 8     <p th:text="'Hello, ' + ${xname} + '!'" />
 9     <div>1234567890!!!xx</div>
10 </body>
11 </html>
View Code

注意

  • src/main/resources/templates:页面存放目录
  • src/main/resources/static:方式静态文件(css、js等)

以上的目录与ssm中开发的不一样,ssm中会放在src/main/webapp下

 

测试:

  • 修改类-->保存:应用会重启
  • 修改配置文件-->保存:应用会重启
  • 修改页面-->保存:应用不会重启,但会重新加载,页面会刷新(原理是将spring.thymeleaf.cache设为false)

补充:

  • 默认情况下,/META-INF/maven,/META-INF/resources,/resources,/static/templates,/public这些文件夹下的文件修改不会使应用重启,但是会重新加载(devtools内嵌了一个LiveReload server,当资源发生改变时,浏览器刷新)。
    • 如果想改变默认的设置,可以自己设置不重启的目录:spring.devtools.restart.exclude=static/**,public/**,这样的话,就只有这两个目录下的文件修改不会导致restart操作了。
    • 如果要在保留默认设置的基础上还要添加其他的排除目录:spring.devtools.restart.additional-exclude
  • 如果想要使得当非classpath下的文件发生变化时应用得以重启,使用:spring.devtools.restart.additional-paths,这样devtools就会将该目录列入了监听范围。

 

参考:http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-exclude

posted on 2016-05-17 17:07  赵计刚  阅读(31883)  评论(9编辑  收藏  举报