Springboot devtools(热部署)

关于devtools热部署是一个提高工作效率的功能,重新部署文件只需要5秒。

在修改,在保存,在配置页面文件时,都会重启。

创建一个meven 项目

向pom.xml中添加依赖包

<!-- spring boot devtools 依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>

  

添加Spring-boot-maven-plugin

  <plugins>
    <build>     
   <!-- 这 是 Spring boot devtool plugin-->
                <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                 <!-- fork: 如果没有配置该项配置,devtools不会起作用的,即应用不会restear -->
                  <fork>ture</fork> 
                 </configuration> 
                </plugin>
        </plugins>
    </build>

  

创建一个class 文件app.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;


import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**使用@SpringBootAplication指定是spring boot的一个应用程序
 * Hello world!
 *
 */	
@SpringBootApplication
//extends WebMvcConfigurerAdapter
public class App 
{

	//在这里我们使用@Bean注入 fastJsonHttpMessageConvert
	@Bean
	public HttpMessageConverters fashJsonHttpMessageConverters(){
		FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
		
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		
		fastConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter =fastConverter;
		
		return new HttpMessageConverters(converter);
		
	}
	
    public static void main( String[] args )
    {
       SpringApplication.run(App.class, args);
    }
}

  之后创建一个controller.java

import java.util.Date;

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

/**
* 使用的RestController 等价于@Controller和@RequestBody
* @author admin
*
*/
@RestController

public class HelloController {
/**
* @RequestMapping:映射到http://127.0.0.1:8080/hello
* @return
*/
@RequestMapping("/hello")
public String hello(){
return "hello";

}

}

  运行后你会发现输出结果为hello

之后更改return "hello" 为return "hello11111"

等待五秒之后,页面刷新输出结果为hello11111

原文地址:https://www.cnblogs.com/LiangPF/p/7085067.html