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:

<?xmlVersion= "1.0" encoding= "UTF-8"?><projectxmlns="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>    <packaging>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 --    </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-devtools</artifactId>            <optional>True</optional>        </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>    <repositories>        <repository>            <id>Spring-snapshots</id>            <url>Https://repo.spring.io/snapshot</url>            <snapshots><enabled>True</enabled></snapshots>        </repository>        <repository>            <id>Spring-milestones</id>            <url>Https://repo.spring.io/milestone</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>Spring-snapshots</id>            <url>Https://repo.spring.io/snapshot</url>        </pluginRepository>        <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;@RestControllerpublicclass HelloController {    @RequestMapping("/")    publichello() {        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;@SpringBootApplicationpublicclass DevtoolsApplication {    publicstaticvoidmain(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->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.