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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM