1. Must restart
In the current Springboot, after any modification occurs, it must be closed before starting the Application class before it can take effect, which seems a little troublesome. Springboot provides a hot deployment method. When any class is found to have changed, the latest class is loaded into the virtual machine immediately through the JVM class loading method. This way you can see the modified effect without restarting
2. pom.xml
The method is very simple, just add a new dependency to pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- This requires true hot deployment to be effective --> </dependency> <?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.how2java</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <packaging>war</packaging> <parent>> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- servlet dependencies. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- support for tomcat.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- This requires true hot deployment to be effective--> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. Restart the test
Restart Application, and then modify HelloController casually, and the automatic restart of the console will be observed as shown in the figure.
Replenish:
Let's take a look at SpringBoot's automatic restart and hot start
Two ways to automatically restart SpringBoot:
1) Add plugin directly to the project's pom, as follows:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <!-- Hot Deployment--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> </dependencies> </plugin> </plugins></build>
When the file operations in classPath (including javadiamante and other configuration files are saved), the project will automatically restart, eliminating the trouble of manually restarting the project;
2) Use the tool class provided by springBoot to add dependencies in the pom;
<dependency> <!--SpringBoot Development Tools--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependency> </dependency>
DevTools is a development tool provided by SpringBoot. After activating the developer tool, any operation on the file in the classpath will trigger the application to restart. SpringBoot developer tools will exclude /META-INF/resources , /resources , /static , /public and /templates when restarting; the Spring.devtools.restart.exclude property can be set to override the default restart exclusion directory;
If you want to turn off automatic restart, you can set Spring.devtools.restart.enable= false as shown in this way; the trigger file must be modified to trigger the restart of the spring.devtools.restart.trigger-file property; the developer tool will be disabled when the application runs in a fully packaged jar or war file. After activating the developer tool, Spring boot will start an embedded LiveReload server, which will trigger refreshing the browser when the resource file changes. All you have to do is install LiveReload in your browser; if you want to exclude the embedded browser Spring.devtools.livereload.enableled=false;
Summarize
The above is the simple method of automatically restarting springboot introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!