第4章 springboot热部署 4-1 SpringBoot 使用devtools进行热部署
/imooc-springboot-starter/src/main/resources/application.properties
#关闭缓存, 即时刷新 #spring.freemarker.cache=false spring.thymeleaf.cache=true #热部署生效 spring.devtools.restart.enabled=true #设置重启的目录,添加那个目录的文件需要restart spring.devtools.restart.additional-paths=src/main/java # 为mybatis设置, 生产环境可删除 #restart.include.mapper=/mapper-[\\w-\\.]+jar #restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar #排除那个目录的文件不需要restart spring.devtools.restart.exclude=static/**,public/**,WEB-INF/** #classpath目录下的WEB-INF文件夹内容修改不重启 #spring.devtools.restart.exclude=WEB-INF/**
/imooc-springboot-starter/pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.imooc</groupId> <artifactId>imooc-springboot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>imooc-springboot-starter</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 热部署 --> <!-- devtools可以实现页面热部署(即页面修改后会立即生效, 这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现) --> <!-- 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 --> <!-- 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机), 注意:因为其采用的虚拟机机制,该项重启是很快的 --> <!-- (1)base classloader (Base类加载器):加载不改变的Class,例如:第三方提供的jar包。 --> <!-- (2)restart classloader(Restart类加载器):加载正在开发的Class。 --> <!-- 为什么重启很快,因为重启的时候只是加载了在开发的Class,没有重新加载第三方的jar包。 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- optional=true, 依赖不会传递, 该项目依赖devtools; 之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 --> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
/imooc-springboot-starter/src/main/java/com/imooc/controller/UserController.java
package com.imooc.controller; import java.util.Date; //import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.imooc.pojo.LeeJSONResult; import com.imooc.pojo.User; //@Controller @RestController // @RestControler = @Controller + @ResponseBody @RequestMapping("/user") public class UserController { //@RequestMapping("/hello") @RequestMapping("/getUser") //@ResponseBody public User hello() { //public User getUser() { User u = new User(); u.setName("imooc"); u.setAge(18); u.setBirthday(new Date()); u.setPassword("imooc"); //u.setDesc(null); u.setDesc("hello imooc~~"); return u; } @RequestMapping("/getUserJson") //@ResponseBody public LeeJSONResult hello1() { //public LeeJsonResult getUserJson() { User u = new User(); //u.setName("imooc"); u.setName("imooc1"); u.setAge(18); u.setBirthday(new Date()); u.setPassword("imooc"); //u.setDesc(null); //u.setDesc("hello imooc~~"); u.setDesc("hello imooc1~~"); return LeeJSONResult.ok(u); } }
/imooc-springboot-starter/src/main/java/com/imooc/controller/UserController.java
package com.imooc.controller; import java.util.Date; //import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.imooc.pojo.LeeJSONResult; import com.imooc.pojo.User; //@Controller @RestController // @RestControler = @Controller + @ResponseBody @RequestMapping("/user") public class UserController { //@RequestMapping("/hello") @RequestMapping("/getUser") //@ResponseBody public User hello() { //public User getUser() { User u = new User(); //u.setName("imooc"); u.setName("imooc1"); u.setAge(18); u.setBirthday(new Date()); //u.setPassword("imooc"); u.setPassword("imooc1"); //u.setDesc(null); //u.setDesc("hello imooc~~"); u.setDesc("hello imooc1~~"); return u; } @RequestMapping("/getUserJson") //@ResponseBody public LeeJSONResult hello1() { //public LeeJsonResult getUserJson() { User u = new User(); //u.setName("imooc"); u.setName("imooc1"); u.setAge(18); u.setBirthday(new Date()); u.setPassword("imooc"); //u.setDesc(null); //u.setDesc("hello imooc~~"); u.setDesc("hello imooc1~~"); return LeeJSONResult.ok(u); } }