Automatic restart and automatic page refresh using spring boot developer tools

Source: Internet
Author: User

Brief introduction

As you may have heard, developing a node. JS application can be facilitated by a variety of tools, such as Webpack, which provides a developer server to support the dynamic alternation of JS applications and automatically refreshes the browser when the file is saved. Spring boot also provides similar developer tools that allow us to develop spring boot applications faster and more enjoyable. You can learn how to use the Spring Boot developer tool to automatically restart and refresh the page automatically as you go through this tutorial.

Automatic restart Principle

The Spring Boot developer tool will create two ClassLoader for your app. One is used to load a class that does not change, called base ClassLoader. The other is restart ClassLoader, which is used to load frequently changing classes, by default the Spring Boot developer tool monitors all classes under Classpath. When there is a class change, the old restart ClassLoader is discarded and then a new one is created to speed up the restart.

Basic Environment
    • JDK 1.8
    • Maven 3.3.9
    • IntelliJ 2018.1
    • Git
Project Source

Gitee Code Cloud

Create a project

Create a MAVEN project using IntelliJ:

    • groupdId:zxuqian.cn
    • Artifactid:devtools

Use the following pom.xml file configuration:

<?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>cn.zxuqian</ groupid> <artifactId>devtools</artifactId> <version>0.0.1-SNAPSHOT</version> <packa Ging>jar</packaging> <name>devtools</name> <description>showcase Project for Spring Boot        Developer Tools</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.build-snapshot</version        > <relativePath/> <!--lookup parent from repository to </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> &LT;DEP Endencies> <dependency> <groupId>org.springframework.boot</groupId> < Artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> & Lt;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid&gt            ; <optional>true</optional> </dependency> <dependency> <groupid>org.s Pringframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> &L            t;scope>test</scope> </dependency> </dependencies> <build> <plugins>             <plugin>   <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-plugin</a rtifactid> </plugin> </plugins> </build> <repositories> <repo Sitory> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url&            Gt <snapshots><enabled>true</enabled></snapshots> </repository> <repository&gt            ; <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repo sitory> </repositories> <pluginRepositories> <pluginRepository> &LT;ID&GT;SPR Ing-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginrepository&gt        ; <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories></project> 

Here we use the latest snapshop version of Spring boot 2.1.0, and then add the spring-boot-devtools dependencies and set it up optional , so that in the final packaged product environment, Devtools will not be packaged in.

Next, add a test controller cn.zxuqian.devtools.controller.HelloController :

package cn.zxuqian.devtools.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @RequestMapping("/")    public String hello() {        return "hello world";    }}

Then create the cn.zxuqian.devtools.DevtoolsApplication class to configure the Spring boot app:

package cn.zxuqian.devtools;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DevtoolsApplication {    public static void main(String[] args) {        SpringApplication.run(DevtoolsApplication.class, args);    }}
Installing the Livereload Plugin

Download the livereload chrome or Firefox or Safari plugin from the Livereload website and enable the plugin.

Test

Use the spring-boot:run plugin to start the app, and you'll see the word when the browser opens http://localhost:8080 hello world . Then in our controller to change the return value, such as: Hola! , in IntelliJ, we have to execute in order to recompile Build-&gt;Build Project the new changes in the code, we can also use the shortcut key command + (fn) + F9 Mac, to perform the compilation. Wait a few seconds and you'll see the browser automatically refreshes to the modified value.

Welcome to my blog: Zhang Xu's Blog

Automatic restart and automatic page refresh using spring boot developer tools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.