溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用@Value注解從配置文件中讀取數(shù)組的方法

發(fā)布時間:2021-07-06 11:27:01 來源:億速云 閱讀:2143 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“使用@Value注解從配置文件中讀取數(shù)組的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“使用@Value注解從配置文件中讀取數(shù)組的方法”吧!

@Value注解從配置文件讀取數(shù)組

作用:從配置文件中取值

用法:

1.取單個值

(1)configuration.properties配置

status.notice.switch=open

(2)java文件自動注入

@Value("${status.notice.switch}")
private String statusNoticeSwitch;

2.取數(shù)組

(1)configuration.properties配置

lanwon.hospital.id=43534,234543,w353654

(2)java文件自動注入

@Value("#{'${lanwon.hospital.id}'.split(',')}")
private List<String> hospitalIdList;

使用@Value注解注入值(配置文件讀?。?/h3>

在 Spring 組件中使用 @Value 注解的方式,可以直接從 .properties,.yum 等配置文件獲取配置信息便于實現(xiàn)項目的配置化運行。

1. 配置方式

1.1 使用

1、@Value("${key}")

2、@Value("#{configProperties[‘key']}") (SpEL表達式)

1.2 默認(rèn)值配置

1、基礎(chǔ)方式: ${key}:defaultvalue

2. SpEL方式:

使用 Spring Expression Language (SpEL) 設(shè)置默認(rèn)值。

下面的代碼標(biāo)示在systemProperties屬性文件中,如果沒有設(shè)置 some.key 的值,my default system property value 會被設(shè)置成默認(rèn)值。

@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;

2. 使用場景

2.1 聲明的變量

public static class FieldValueTestBean {
  @Value("#{ systemProperties['user.region'] }")
 private String defaultLocale;
}

2.2 setter 方法

public static class PropertyValueTestBean {
    private String defaultLocale;
    @Value("#{ systemProperties['user.region'] }")
    public void setDefaultLocale(String defaultLocale) {
        this.defaultLocale = defaultLocale;
    } 
}

2.3 方法

public class SimpleMovieLister {
    private MovieFinder movieFinder;
    private String defaultLocale;
    @Autowired
    public void configure(MovieFinder movieFinder,
            @Value("#{ systemProperties['user.region'] }") String defaultLocale) {
        this.movieFinder = movieFinder;
        this.defaultLocale = defaultLocale;
    }
    // ...
}

2.4 構(gòu)造方法

public class MovieRecommender { 
    private String defaultLocale; 
    private CustomerPreferenceDao customerPreferenceDao; 
    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
            @Value("#{systemProperties['user.country']}") String defaultLocale) {
        this.customerPreferenceDao = customerPreferenceDao;
        this.defaultLocale = defaultLocale;
    } 
    // ...
}

3 各種屬性的注入及其默認(rèn)值設(shè)置

3.1 字符串

字符串類型的屬性設(shè)置默認(rèn)值。

@Value("${some.key:my default value}")
private String stringWithDefaultValue;

some.key 沒有設(shè)置值,stringWithDefaultValue 變量值將會被設(shè)置成 my default value 。

如果默認(rèn)值設(shè)為空,也將會被設(shè)置成默認(rèn)值。

@Value("${some.key:}")
private String stringWithBlankDefaultValue;

3.2 基本類型

基本類型設(shè)置默認(rèn)值。

@Value("${some.key:true}")
private boolean booleanWithDefaultValue;
@Value("${some.key:42}")
private int intWithDefaultValue;

3.3 包裝類型

包裝類型設(shè)置默認(rèn)值。

@Value("${some.key:true}")
private Boolean booleanWithDefaultValue;
 
@Value("${some.key:42}")
private Integer intWithDefaultValue;

3.4 數(shù)組

數(shù)組的默認(rèn)值可以使用逗號分割。

@Value("${some.key:one,two,three}")
private String[] stringArrayWithDefaults;
 
@Value("${some.key:1,2,3}")
private int[] intArrayWithDefaults;

到此,相信大家對“使用@Value注解從配置文件中讀取數(shù)組的方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI