springBoot(4)---熱部署,配置文件使用
熱部署,配置文件使用
一、熱加載
spring為開發者提供了一個名為spring-boot-devtools的模塊來使Spring Boot應用支持熱部署,提高開發者的開發效率,無需手動重啟Spring Boot應用。
devtools的原理
深層原理是使用了兩個ClassLoader,一個Classloader加載那些不會改變的類(第三方Jar包),另一個ClassLoader加載會更改的類,稱為restart ClassLoader,這樣在有代碼更改的時候,原來的restart ClassLoader 被丟棄,重新創建一個restart ClassLoader,由於需要加載的類相比較少,所以實現了較快的重啟時間。
官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools
實現熱部署,首先要引入:spring-boot-devtools.jar包
核心依賴包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId><optional>true</optional> </dependency>
添加依賴後,在ide裏面重啟應用,後續修改後馬上可以生效
默認不被熱部署的文件
1、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates
2、指定文件不進行熱部署 spring.devtools.restart.exclude=static/**,public/**
在開發中,我們會思考一個問題?
如果你寫一個邏輯代碼,需要好幾個文件,總不能你每保存一次就進行一次熱部署,這裏有個解決方法。
在application.properties添加手工觸發重啟
#指定某些文件不進行監聽,即不會進行熱加載 #spring.devtools.restart.exclude=application.properties #通過觸發器,去控制什麽時候進行熱加載部署新的文件 spring.devtools.restart.trigger-file=trigger.txt
然後在src\main\resources目錄下,添加trigger.txt文件
version=1
這樣你每次改好代碼,不會每次保存就熱部署,而是改好代碼後,改version=2就會進行熱部署。
註意點:生產環境不要開啟這個功能,如果用java -jar啟動,springBoot是不會進行熱部署的
二、SpringBoot註解把配置文件自動映射到屬性和實體類實戰
方式一、Controller上面配置
簡介:講解使用@value註解配置文件自動映射到屬性和實體類
1、配置文件加載
方式一
1、Controller上面配置
@PropertySource({"classpath:resource.properties"})
2、增加屬性
@Value("${test.name}")
private String name;
舉例
上篇的文件上傳的地址我是寫死的。
這樣顯然不科學,這裏改成寫在1.application.properties配置文件裏。
#文件上傳路徑配置
web.file.path=C:/Users/chenww/Desktop/springbootstudy/springbootstudy/src/main/resources/static/images/
2在FileController 類中
@Controller @PropertySource({"classpath:application.properties"}) public class FileController { //文件放在項目的images下 // private String filePath = "C:\\Users\\chenww\\Desktop\\springbootstudy\\springbootstudy\\src\\main\\resources\\static\\images\\"; @Value("${web.file.path}") private String filePath;
總結:
1:@PropertySource代表讀取哪個文件
2:@Value通過key得到value值
方式二:實體類配置文件
步驟:
1、添加 @Component 註解;
2、使用 @PropertySource 註解指定配置文件位置;
3、使用 @ConfigurationProperties 註解,設置相關屬性;
4、必須 通過註入IOC對象Resource 進來 , 才能在類中使用獲取的配置文件值。
@Autowired
private ServerSettings serverSettings;
案例:
1.在application.properties
#測試配置文件路徑 test.domain=www.jincou.com test.name=springboot
2.創建ServerSettings實體
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; //服務器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties public class ServerSettings { //名稱test.domain是key值 @Value("${test.domain}") private String name; //域名地址 @Value("${test.name}") private String domain; //提供set和get方法 }
三創建GetController
import com.jincou.model.ServerSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GetController { //需要註入 @Autowired private ServerSettings serverSettings; @GetMapping("/v1/test_properties") public Object testPeroperties(){ return serverSettings; } }
頁面效果
其實上面還可以做個優化:
創建ServerSettings實體
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; //服務器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties(prefix="test") //這裏加了個test前綴 public class ServerSettings { //這是不需要寫vlaue標簽,只要text.name去掉前綴test後的name和這裏name相同,就會自動賦值 private String name; //域名地址 private String domain; //提供set和get方法 }
想太多,做太少,中間的落差就是煩惱。想沒有煩惱,要麽別想,要麽多做。上尉【6】
springBoot(4)---熱部署,配置文件使用