1. 程式人生 > 其它 >Springboot讀取別的配置檔案

Springboot讀取別的配置檔案

前提

springboot專案需要讀取非application.yml/properties 的配置檔案。

操作步驟

  1. 新建配置檔案
    在這裡插入圖片描述
  2. 編輯配置檔案
test-server=rd-dev02.jr.rong360.com
  1. 新建Config類
@Component
@PropertySource(value = "kirara.properties")
public class KiraraConfig {

    @Value("${test-server:rd-dev02.jr.rong360.com}")
    private String testServer;
public String getTestServer() { return testServer; } public void setTestServer(String testServer) { this.testServer = testServer; } }
  1. 編輯呼叫類
@RestController
public class UuapLoginController {

    @Autowired
    private UuapLoginService loginService;
    
    @Autowired
private KiraraConfig kiraraConfig; /** * 登入方法 * * @param loginBody 登入資訊 * @return 結果 */ @PostMapping("/api/v1/login") public AjaxResult login() throws Exception { AjaxResult ajax = AjaxResult.success(); kiraraConfig.getTestServer()
; return ajax; } }

總結

主要是用Config類去載入配置檔案內容