Create a new Maven Project  and  we have two class under the package com.example.demo like below screen shot.

This page mainly focus on how to make the change you make ready to show the result without restarting  spring boot application manually.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController {
@RequestMapping(value = "hi" , method = RequestMethod.GET)
public String sayHi() {
return "Hello World!";
} }
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</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-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

Then,run the DemoApplication as Java Application or Spring Boot App (if you are using STS),after the application starts up,input the link localhost:8080/hi ,you will get the Hello World! as the output.

But when you want to change the RequestMapping from "hi" to "hello" ,to make it ready to use you need to change it and restart the application.

with spring-boot-devtools we just change the code and save it without restart the application manually,spring boot help us restart the application .

To achieve this , all you need to do are adding  the spring-boot-devtools dependency in the pom.xml and make sure Project>build automatically is chosen in the IDE

这里多了一个Optional选项,是为了防止将 devtools 依赖传递到其他模块中。当将应用打包运行后,devtools 会被自动禁用。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

On the condition that you add the dependency in the pom.xml file and you don't choose the build automatically within the IDE(Eclipse or STS here),then when you change the the RequestMapping from "hi" to "hello" and save it,spring boot will not restart the application for you ,you have to bulid the pom file and restart the application manualy.Because the spring-boot-devtools monitor the code under the target to restart the application to make the change you made ready to use ,so you choose the build atuomatically and change RequestMapping and save it ,the IDE build and the code under the target is  updated to trigger the restart of the spring boot application automatically.

For more details,please refer the spring boot  devtools reference guide : http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html

由于项目的编码是一个连续的过程,并不需要每改一行代码就重启项目。

此时,可以使用触发文件,在application.yml 中配置spring.devtools.restart.trigger

然后在src/main/resource 下创建 .trigger-file

此时,当开发者修改代码时,不会重启项目;需要重启项目时,开发者需要在修改代码并保存之后,修改 .trigger-file, 项目就会重启。

如果项目没有改变,只是改变了 .trigger-file 文件,项目不会重启。

SpringBoot 中使用的自动重启技术涉及两个类加载器,一个是 baseclassloader, 用来加载不会变化的类,例如项目应用的第三方Jar 包;

另一个是 restartclassloader,用来加载开发者自己写的会变化的类。

当改变自己写的代码项目需要重启时,restartclassloader 将被一个新创建的类加载器代替,而 baseclassloader 则继续使用原来的,这种启动方式要比冷启动快很多,因为  baseclassloader 已经存在并且已经加载好。

当改变POM 文件中的依赖,项目会重启,因为 baseclassloader 会重新加载所依赖的 Jar包。

spring-boot-devtools的更多相关文章

  1. 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)

    第一步:添加springboot的配置文件 首先我先贴出我的配置 添加依赖包 <!-- spring boot devtools 依赖包. --> <dependency> & ...

  2. 使用Spring Boot DevTools优化你的开发体验

    场景再现 某日少年收到前端同学发来的消息说联调的接口响应异常

  3. spring boot devtools热部署

    问题1: Springloaded 在springboot2的maven的pom.xml 无法找到 解决方法:在idea通过View->Tool Windows->Maven Projec ...

  4. IntelliJ IDEA Spring boot devtools 实现热部署

    一.spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动部署新代码. 二.原理 使用了两个ClassLoader,一个ClassLoader用来加载那些不会变 ...

  5. Spring Boot 系列教程5-热部署-devtools模块

    devtools模块 devtools模块,是为开发者服务的一个模块.主要的功能就是代码修改后一般在5秒之内就会自动重新加载至服务器,相当于restart成功. 原理 简单原理 在发现代码有更改之后, ...

  6. Spring Boot 面试题

    1.列举一些SpringBoot特性 1.创建独立的Spring项目 2.内置Tomcat和Jetty容器 3.提供一个starter POMs来简化Maven配置 4.提供了一系列大型项目中常见的非 ...

  7. 2019阿里P7最新总结Spring Boot面试问题

        Spring Boot一直是Spring生态系统的关键参与者.该项目通过其自动配置功能使我们的生活更加轻松.在本教程中,我们将介绍在求职面试中可能出现的一些与Spring Boot相关的最常见 ...

  8. spring boot:创建一个简单的web(maven web project)

    1.新建一个maven web project; 2.在pom.xml文件中添加相应的依赖包: 3.新建一个HelloController请求控制类: 4.编写index.jsp页面: 5.编写启动类 ...

  9. spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化

    SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的) 1在pom.xml引入mysql, spring-data-jpa依赖 2.在src/ ...

  10. spring boot: 热部署spring-boot-devtools

    spring boot: 热部署spring-boot-devtools 1引入spring-boot-devtools依赖包 <!-- spring boot devtools 热部署 --& ...

随机推荐

  1. arm_linux_device_mem内存映射

    /dev/mem: 物理内存的全镜像.可以用来访问物理内存. /dev/kmem: kernel看到的虚拟内存的全镜像.可以用来访问kernel的内容. /dev/mem 用来访问物理IO设备比如X ...

  2. 用SDL库播放yuy2 Packed mode

    #define SDL_YUY2_OVERLAY 0x32595559 /* Packed mode: Y0+U0+Y1+V0 */ if (SDL_Init(SDL_INIT_VIDEO) < ...

  3. Nginx学习笔记3--Nginx和PHP(fastCGI)的配置和优化

    php和nginx的配置 server{ root html; listen 80; server_name a.com; index index.php index.html ; location ...

  4. INF 右键安装驱动以及卸载

    INF 右键安装驱动以及卸载 之前写过一篇文章是关于INF文件详解的,大家可以参看INF文件详解,这次写的是关于INF右键安装,这样比较方便.卸载的话也是一句话,可以大大减少安装时间: 先将INF文件 ...

  5. ftp搭建 与http服务访问

    Linux安装ftp组件 1 FTP http://jingyan.baidu.com/article/380abd0a77ae041d90192cf4.html安装vsftpd组件 安装完后,有/e ...

  6. Java并发 线程池

    线程池技术就是事先创建一批线程,这批线程被放入到一个池子里,在没有请求到达服务端时候,这些线程都是处于待命状态,当请求到达时候,程序会从线程池里取出一个线程,这个线程处理到达的请求,请求处理完毕,该线 ...

  7. 微信开发中access_token,js_ticket,时间戳,签名生成工具

    Access_token生成工具 工具地址:https://mp.weixin.qq.com/debug 这个工具主要用来验证和生成微信公众号access_token,主要有正确的app_id和app ...

  8. 从“跳一跳”来看微信小程序的未来

    从“跳一跳”来看微信小程序的未来   相信大家这两天都被微信新推出的小程序跳一跳刷爆了朋友圈,为了方便用户在使用过程中切换小程序,微信在这次6.6.1版本中加入了下拉可快速切换小程序的功能,而“跳一跳 ...

  9. 说说你对用SSH框架进行开发的理解

    SSH框架指的是Struts,Spring,Hibernate.其中,Struts主要用于流程控制:Spring的控制反转能祈祷解耦合的作用:Hibernate主要用于数据持久化.

  10. 【BZOJ1911】【APIO2010】特别行动队(斜率优化,动态规划)

    [BZOJ1911][APIO2010]特别行动队 题面 Description 你有一支由 n 名预备役士兵组成的部队,士兵从 1 到 n 编号, 要将他们拆分成若干特别行动队调入战场.出于默契的考 ...