欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot獲取yml和properties配置文件的內(nèi)容

 更新時間:2021年08月18日 15:25:42   作者:彩虹過后的羽翼  
這篇文章主要為大家詳細介紹了SpringBoot獲取yml和properties配置文件的內(nèi)容,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了SpringBoot獲取yml和properties配置文件的具體代碼,供大家參考,具體內(nèi)容如下

(一)yml配置文件:

pom.xml加入依賴:

<!-- 支持 @ConfigurationProperties 注解 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <version>${spring-boot.version}</version>
</dependency>

在application.yml文件中加上:

#自定義的屬性和值
myYml:
 simpleProp: simplePropValue
 arrayProps: 1,2,3,4,5
 listProp1:
  - name: abc
   value: abcValue
  - name: efg
   value: efgValue
 listProp2:
  - config2Value1
  - config2Vavlue2
 mapProps:
  key1: value1
  key2: value2

使用一個java類獲取yml文件的內(nèi)容:

package com.sun.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加載yaml配置文件的方法
 * Created by sun on 2017-1-15.
 * spring-boot更新到1.5.2版本后locations屬性無法使用
 * @PropertySource注解只可以加載proprties文件,無法加載yaml文件
 * 故現(xiàn)在把數(shù)據(jù)放到application.yml文件中,spring-boot啟動時會加載
 */
@Component
//@ConfigurationProperties(locations = {"classpath:config/myProps.yml"},prefix = "myProps")
@ConfigurationProperties(prefix = "myYml")
public class YmlConfig {

  String simpleProp;
  private String[] arrayProps;
  private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的屬性值
  private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的屬性值
  private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的屬性值

  public String getSimpleProp() {
    return simpleProp;
  }

  //String類型的一定需要setter來接收屬性值;maps, collections, 和 arrays 不需要
  public void setSimpleProp(String simpleProp) {
    this.simpleProp = simpleProp;
  }

  public String[] getArrayProps() {
    return arrayProps;
  }

  public void setArrayProps(String[] arrayProps) {
    this.arrayProps = arrayProps;
  }

  public List<Map<String, String>> getListProp1() {
    return listProp1;
  }

  public void setListProp1(List<Map<String, String>> listProp1) {
    this.listProp1 = listProp1;
  }

  public List<String> getListProp2() {
    return listProp2;
  }

  public void setListProp2(List<String> listProp2) {
    this.listProp2 = listProp2;
  }

  public Map<String, String> getMapProps() {
    return mapProps;
  }

  public void setMapProps(Map<String, String> mapProps) {
    this.mapProps = mapProps;
  }
}

通過依賴注入就可以獲取該對象:

@Autowired
private YmlConfig config;

方法內(nèi)獲取值:

ObjectMapper objectMapper = new ObjectMapper();
//測試加載yml文件
System.out.println("simpleProp: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));

(二)properties配置文件:

使用@PropertySource注解加載配置文件,該注解無法加載yml配置文件。使用@Value注解獲得文件中的參數(shù)值

package com.sun.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**
 * 加載properties配置文件,在方法中可以獲取
 * abc.properties文件不存在,驗證ignoreResourceNotFound屬性
 * 加上encoding = "utf-8"屬性防止中文亂碼,不能為大寫的"UTF-8"
 * Created by sun on 2017-3-30.
 */
@Configuration
@PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/abc.properties"},
    ignoreResourceNotFound = true,encoding = "utf-8")
public class PropConfig {

  // PropertySourcesPlaceholderConfigurer這個bean,
  // 這個bean主要用于解決@value中使用的${…}占位符。
  // 假如你不使用${…}占位符的話,可以不使用這個bean。
  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }
}
//獲取properties文件參數(shù)值有兩種方法,一種獲得Environment 的對象,第二種就是@Value注解

@Autowired
  private Environment env;
  @Value("${age}")
  String name;


  @RequestMapping("/")
  @ResponseBody
  String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
    logger.info("測試通過?。?!");
    ObjectMapper objectMapper = new ObjectMapper();
    //測試加載yml文件
    System.out.println("simpleProp: " + config.getSimpleProp());
    System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
    System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
    System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
    System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));

    //測試加載properties文件
    System.out.println(env.getProperty("name"));//孫凱
    System.out.println(env.getProperty("abc"));//null
    System.out.println(name);//26

    return "Hello World!";
  }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java利用cors實現(xiàn)跨域請求實例

    Java利用cors實現(xiàn)跨域請求實例

    本篇文章主要介紹了Java利用cors實現(xiàn)跨域請求實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Spring Boot實戰(zhàn)教程之自動配置詳解

    Spring Boot實戰(zhàn)教程之自動配置詳解

    Spring Boot的自動配置給開發(fā)者帶來了很大的便利,當開發(fā)人員在pom文件中添加starter依賴后,maven或者gradle會自動下載很多jar包到classpath中。下面這篇文章主要給大家介紹了關(guān)于Spring Boot自動配置的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • Mybatis如何實現(xiàn)InsertOrUpdate功能

    Mybatis如何實現(xiàn)InsertOrUpdate功能

    這篇文章主要介紹了Mybatis如何實現(xiàn)InsertOrUpdate功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 用java實現(xiàn)在txt文本中寫數(shù)據(jù)和讀數(shù)據(jù)的方法

    用java實現(xiàn)在txt文本中寫數(shù)據(jù)和讀數(shù)據(jù)的方法

    今天小編就為大家分享一篇用java實現(xiàn)在txt文本中寫數(shù)據(jù)和讀數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Java中時間API的基本使用教程

    Java中時間API的基本使用教程

    這篇文章主要介紹了Java中時間API的基本使用教程,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • Spring @Lookup深入分析實現(xiàn)原理

    Spring @Lookup深入分析實現(xiàn)原理

    這篇文章主要介紹了Spring @Lookup實現(xiàn)原理,我們知道在spring容器中單獨的一個抽象類是不能成為一個bean的,那么有沒有辦法呢?這個時候我們可以使用Lookup注解
    2023-01-01
  • Java8的常用時間api實用指南

    Java8的常用時間api實用指南

    這篇文章主要給大家介紹了關(guān)于Java8的常用時間api的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • 淺談Java基礎(chǔ)知識之BigDecimal

    淺談Java基礎(chǔ)知識之BigDecimal

    我們又來回顧Java基礎(chǔ)知識啦,今天講的是BigDecimal的基本使用以及異常處理,下文中有非常詳細的代碼示例以及注釋哦,需要的朋友可以參考下
    2021-05-05
  • Java實踐練習輕松幾行實現(xiàn)追書神器

    Java實踐練習輕松幾行實現(xiàn)追書神器

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個追書神器,用技術(shù)改變生活,大家可以在過程中查缺補漏,提升水平
    2021-10-10
  • Java創(chuàng)建類模式_動力節(jié)點Java學院整理

    Java創(chuàng)建類模式_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了Java創(chuàng)建類模式的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論