日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

jackson 读取多文件_Spring Boot系列之读取配置

發(fā)布時間:2025/3/11 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jackson 读取多文件_Spring Boot系列之读取配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用SpringBoot框架開發(fā),讀取配置是少不了的,那么你會讀取配置嗎?你會寫配置嗎?List?Map?

1 目的

本節(jié)我們要解決如下幾個問題:

  • 如何使用Spring Boot讀取配置文件?有哪些方式?

  • 常用的幾種數(shù)據(jù)結(jié)構(gòu),如字符串、整數(shù)、List、Map,如何配置?如何讀取?

  • 如何自定義配置文件的路徑?

2 讀配置文件


Spring Boot默認(rèn)的配置文件有兩種格式:?application.properties?和?application.yml?。

查找順序是首先從application.properties?查找,如果找不到,再查找?application.yml。


優(yōu)先級:?application.properties > application.yml?。

2.1 使用?@Value 讀取配置

配置如下:

erwin.name=馮文議
erwin.age=20
erwin.sex=男
erwin.english-name=Erwin Feng
erwin.birthday=1992/02/26
erwin.like=movie,game,music,tea,travel
erwin.visitedCities=巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京
erwin.moreOther={myWeb:'https://fengwenyi.com',github:'https://github.com/fengwenyi'}

代碼如下:

package com.fengwenyi.spring_boot_config_sample.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
* @author Erwin Feng
* @since 2020/8/11
*/
@Data
@Configuration
public class ReadConfigByValue {

@Value("${erwin.name}")
private String name;

@Value("${erwin.age}")
private Integer age;

@Value("${erwin.sex}")
private String sex;

@Value("${erwin.english-name}")
private String englishName;

@Value("${erwin.birthday}")
private Date birthday;

@Value("${erwin.like}")
private List likes;@Value("#{'${erwin.visitedCities}'.split(',')}")private List visitedCities;@Value("#{${erwin.moreOther}}")private Map moreOther;
}

2.2 使用 @ConfigurationProperties 讀取配置

配置如下(properties格式)

author.name=馮文議
author.age=20
author.sex=男
author.english-name=Erwin Feng
author.birthday=1992/02/26
author.like[0]=movie
author.like[1]=game
author.like[2]=music
author.like[3]=tea
author.like[4]=travel
author.visitedCities=巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京
author.moreOther.myWeb=https://fengwenyi.com
author.moreOther.github=https://github.com/fengwenyi

配置如下(yaml格式)

author:
name: 馮文議-yml
age: 20
sex: 男
english-name: Erwin Feng
birthday: 1992/02/26
like:
- movie
- game
- music
- tea
- travel
visitedCities: 巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京
moreOther:
myWeb: https://fengwenyi.com
github: https://github.com/fengwenyi


代碼如下:

package com.fengwenyi.spring_boot_config_sample.config;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Erwin Feng
* @since 2020/8/12
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "author")
public class AuthorConfig implements Serializable {

private static final long serialVersionUID = 9032405467573421607L;

private String name;

private Integer age;

private String sex;

private String englishName;

@JsonFormat(pattern = "yyyy/MM/dd")
private Date birthday;

private List like;private List visitedCities;private Map moreOther;
}


讀取出來的數(shù)據(jù)展示:

{
"name":"馮文議",
"age":20,
"englishName":"Erwin Feng",
"birthday":"1992/02/26",
"likes":[
"movie",
"game",
"music",
"tea",
"travel"
],
"visitedCities":[
"巴中",
"揭陽",
"廣州",
"從化",
"成都",
"三亞",
"上海",
"杭州",
"北京"
],
"moreOther":{
"myWeb":"https://fengwenyi.com",
"github":"https://github.com/fengwenyi"
}
}

2.3 通過注入環(huán)境變量來獲取配置信息

@Autowired
private Environment environment;

3 @Value 用法

3.1 設(shè)置默認(rèn)值

格式:@Value("${name:defaultValue}")

當(dāng)配置文件找不到這個配置時,就會返回默認(rèn)值,如果沒有默認(rèn)值,就會報錯。

3.2 可以直接讀取系統(tǒng)的屬性值

如:@Value("${java.home}")

D:JavaJava8jdk1.8.0_251jre

3.3 可以用在方法和參數(shù)上,當(dāng)做單元測試

// 單元測試-讀取配置文件的值
@Value("${erwin.like}")
public void testReadLike(String like, @Value("${erwin.sex}") String sex) {
System.out.println("1===>" + like);
System.out.println("1===>" + sex);
}參數(shù) like 會取 @Value("${erwin.like}") 的值
參數(shù) sex 會取 @Value("${erwin.sex}") 的值
經(jīng)過測試,多個方法,執(zhí)行順序是隨機。
特別注意:這個只是在啟動的時候執(zhí)行,但是實際調(diào)用的時候,還是傳入的值。

3.4 讀取list的值

方法一:
配置文件:

erwin.like=movie,game,music,tea,travel


Java代碼:

@Value("${erwin.like}")
private List likes;

方法二:

erwin.visitedCities=巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京

Java代碼:

@Value("#{'${erwin.visitedCities}'.split(',')}")
private List visitedCities;

3.5 讀取Map的值

配置文件:

erwin.moreOther={myWeb:'https://fengwenyi.com',github:'https://github.com/fengwenyi'}

Java代碼:

@Value("#{${erwin.moreOther}}")
private Map moreOther;

3.6 讀取系統(tǒng)屬性

@Value("#{systemProperties}")
private Map systemPropertiesMap;

3.7 給私有屬性賦值

@Component
@PropertySource("classpath:values.properties")
public class PriorityProvider {

private String priority;

@Autowired
public PriorityProvider(@Value("${priority:normal}") String priority) {
this.priority = priority;
}

// standard getter
}

4 @ConfigurationProperties

package com.fengwenyi.spring_boot_config_sample.config;

import com.fengwenyi.spring_boot_config_sample.support.YamlPropertySourceFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.Serializable;

/**
* @author Erwin Feng
* @since 2020/8/13
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "db")
//@PropertySource({"classpath:config/db.properties"})
@PropertySource(value = {"classpath:config/db.yml"}, factory = YamlPropertySourceFactory.class)public class DBConfig implements Serializable {

private static final long serialVersionUID = -6527591545525817929L;

/** 服務(wù)器地址 */
private String host;

/** 端口 */
private Integer port;

/** 數(shù)據(jù)庫名 */
private String dbName;

/** 用戶名 */
private String username;

/** 密碼 */
private String password;
}

配置文件:

db:
host: localhost
port: 3306
db-name: test
username: root
password: 123456說明:
1、@Configuration 表明這是一個Spring配置,會注入到Spring容器中。
2、@ConfigurationProperties(prefix = "db") 表示這個類與配置文件關(guān)聯(lián),其中prefix指明前綴。
3、@PropertySource 這個指明配置文件的位置,如果沒有這個配置,則會從默認(rèn)的配置文件讀取。默認(rèn)的配置文件:application.properties > application.yml。另外,這個默認(rèn)只支持properties類型的文件,所以,需要配置factory。
4、@PropertySource也可以與@Value結(jié)合使用。

參考鏈接

  • @Value用法:https://www.baeldung.com/spring-value-annotation

  • 自定義配置 只讀properties 不讀 yml文件:https://blog.csdn.net/WTUDAN/article/details/103313167

  • Spring配置文件user.name配置無效:https://blog.csdn.net/u013536829/article/details/80027391

關(guān)于

我是馮文議(Erwin Feng),Java Developer,專注于程序設(shè)計與開發(fā)。開源項目:JavaLib、api-result。喜歡電影、游戲、音樂、茶、旅行。
我的個人網(wǎng)站:https://fengwenyi.com
我的Github:https://github.com/fengwenyi

總結(jié)

以上是生活随笔為你收集整理的jackson 读取多文件_Spring Boot系列之读取配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。