Springboot讀取別的配置檔案
阿新 • • 發佈:2021-05-29
前提
springboot專案需要讀取非application.yml/properties 的配置檔案。
操作步驟
- 新建配置檔案
- 編輯配置檔案
test-server=rd-dev02.jr.rong360.com
- 新建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;
}
}
- 編輯呼叫類
@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類去載入配置檔案內容