亚洲AVI,黑人巨茎大战欧美白妇,初高中生洗澡自慰高清网站,欧美日韩无砖专区一中文字

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)

springboot啟動(dòng)時(shí)加載外部配置文件的方法

前言

創(chuàng)新互聯(lián)長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為自貢企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站制作,自貢網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

相信很多人選擇Spring Boot主要是考慮到它既能兼顧Spring的強(qiáng)大功能,還能實(shí)現(xiàn)快速開發(fā)的便捷。本文主要給大家介紹了關(guān)于spring boot啟動(dòng)時(shí)加載外部配置文件的相關(guān)內(nèi)容,下面話不多說了,來隨著小編一起學(xué)習(xí)學(xué)習(xí)吧。

業(yè)務(wù)需求:

加載外部配置文件,部署時(shí)更改比較方便。

先上代碼:

@SpringBootApplication
public class Application {

 public static void main(String[] args) throws Exception {
  SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class);
  springApplicationBuilder.web(true);
  Properties properties = getProperties();
  StandardEnvironment environment = new StandardEnvironment();
  environment.getPropertySources().addLast(new PropertiesPropertySource("micro-service", properties));
  springApplicationBuilder.environment(environment);
  springApplicationBuilder.run(args);
 }

 private static Properties getProperties() throws IOException {
  PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
  ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  propertiesFactoryBean.setIgnoreResourceNotFound(true);
  Resource fileSystemResource = resolver.getResource("file:/opt/company/test.properties");
  propertiesFactoryBean.setLocations(fileSystemResource);
  propertiesFactoryBean.afterPropertiesSet();
  return propertiesFactoryBean.getObject();
 }
}

使用變量的工具類

@Component
public class EnvironmentUtil {
 private static Environment environment;
 @Autowired
 public void setEnvironment(Environment environment) {
  EnvironmentUtil.environment = environment;
 }

 public static  T getProperty(String key, Class targetType, T defaultValue) {
  return environment.getProperty(key, targetType, defaultValue);
 }

 public static  T getProperty(String key, Class targetType) {
  return environment.getProperty(key, targetType);
 }

 public static String getProperty(String key) {
  return environment.getProperty(key);
 }

 public static String getProperty(String key, String defaultValue) {
  return environment.getProperty(key, defaultValue);
 }

 public static Integer getInteger(String key, Integer defaultValue) {
  return environment.getProperty(key, Integer.class, defaultValue);
 }
}

也可以通過@Value("${key}")使用

這中加載方法優(yōu)先級很高,如果與spring boot配置文件同名,將覆蓋application.properties文件中的配置。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。


本文題目:springboot啟動(dòng)時(shí)加載外部配置文件的方法
網(wǎng)站網(wǎng)址:http://news.spvevtbd.cn/article/jichsp.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP