1. 程式人生 > 實用技巧 >【轉】 springBoot(4)---熱部署,配置檔案使用

【轉】 springBoot(4)---熱部署,配置檔案使用

【轉】 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】