I’ve been thinking about writing a new column about Spring Boot for a long time, but I’ve just had time to update it.

In this first article, I don’t want to just talk about Hello World of Spring Boot, nor do I want to start with a lot of Spring concepts, or even IOC and AOP, like most books. The most rewarding thing for most people is to see localHost appear as they want, rather than to take the trouble to understand the basic concepts. The concepts will be introduced gradually, but let’s get started.

What is hot deployment

Those of you who have read a little bit about Spring Boot know that without hot deployment, every time we make a change in the source code, we have to restart the application service to see what we have changed, which is a huge waste of time and leads to inefficient development.

It’s easier to explain hot deployment, which means upgrading software while the application is running without restarting it. Simply put, no matter how you modify the source code after starting the service, you only need to refresh the browser to appear the modified interface.

For example, setting debug=True in flask is the same as setting debug=True in flask.

Now that you’ve defined hot deployment, let’s look at how to implement hot deployment in Spring Boot.

implementation

The easiest way to do hot deployment in development is to add spring-boot-DevTools dependencies. When we’re not using the template engine (which should not be used much these days because it’s mostly front and back end separation), we just focus on the hot deployment of back-end applications.

  1. Create a new hot deployment SpringBoot project and add the corresponding dependencies as you create it.

file->new->project Select Spring Initializr, then Next. Next, modify the desired Name and Java version, and then nextThe rest of the operation is actually very simple, check whatever dependencies you need, this time we need hot deployment, soDevToolsYou need to choose Lombok or whatever depends on your needs. And then I’m going to add Spring Web to the Web, and there are some Spring Boot starters that I’ll talk about in the future, but now you just need to know that with Spring Web, your project can automatically associate dependencies. Next, change the project name and Finish. Such a new Spring Boot project is ready.

  1. Modify some configuration setting of IDEA in compileCTRL + Shift + Alt +/ (Mac command+ Shift + Option +/), select Registry, check Compiler autoMake allow when APP running

  1. Start the application, and then look at the effect of the change In SRC/main/Java/com. Example. The new Controller folder under the demo, about the content of the MVC is behind, just need to do now. Then create the TestDevToolsController class in Controller as follows
package com.example.demo.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestDevToolsController {
    @RequestMapping("/testDevTools")
    public String testDevTools(a){
        return "test DevTools 122"; }}Copy the code

And then launch the application

Realize the principle of

The most important function of spring-boot-DevTools is to automatically update the modified code to the latest application. The working principle is simply to load unmodified classes and modified classes separately, so that when there are changes, you only need to reload the modified classes, rather than the whole project. This enables a faster restart of the project.

The results of

Open the localhost: 8080 / testDevTools shown belowNow change the TestDevToolsController toreturn "test DevTools 123";Refresh the browser, and the page flashes back to the modified content

Not only will you learn spring Boot’s Hello World, but you’ll also learn how to add dependencies and hot deployment, and then you’ll have to work your way through those complex concepts.