使用@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注解注入值(配置文件讀?。?/h2>
在 Spring 組件中使用 @Value 注解的方式,可以直接從 .properties,.yum 等配置文件獲取配置信息便于實現(xiàn)項目的配置化運行。
1. 配置方式
1.1 使用
1、@Value("${key}")
2、@Value("#{configProperties[‘key']}") (SpEL表達式)
1.2 默認值配置
1、基礎(chǔ)方式: ${key}:defaultvalue
2. SpEL方式:
使用 Spring Expression Language (SpEL) 設(shè)置默認值。
下面的代碼標示在systemProperties屬性文件中,如果沒有設(shè)置 some.key 的值,my default system property value 會被設(shè)置成默認值。
@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 各種屬性的注入及其默認值設(shè)置
3.1 字符串
字符串類型的屬性設(shè)置默認值。
@Value("${some.key:my default value}") private String stringWithDefaultValue;
some.key 沒有設(shè)置值,stringWithDefaultValue 變量值將會被設(shè)置成 my default value 。
如果默認值設(shè)為空,也將會被設(shè)置成默認值。
@Value("${some.key:}") private String stringWithBlankDefaultValue;
3.2 基本類型
基本類型設(shè)置默認值。
@Value("${some.key:true}") private boolean booleanWithDefaultValue;
@Value("${some.key:42}") private int intWithDefaultValue;
3.3 包裝類型
包裝類型設(shè)置默認值。
@Value("${some.key:true}") private Boolean booleanWithDefaultValue; @Value("${some.key:42}") private Integer intWithDefaultValue;
3.4 數(shù)組
數(shù)組的默認值可以使用逗號分割。
@Value("${some.key:one,two,three}") private String[] stringArrayWithDefaults; @Value("${some.key:1,2,3}") private int[] intArrayWithDefaults;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一小時迅速入門Mybatis之實體類別名與多參數(shù) 動態(tài)SQL
這篇文章主要介紹了一小時迅速入門Mybatis之實體類別名與多參數(shù) 動態(tài)SQL,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09JavaWeb頁面中防止點擊Backspace網(wǎng)頁后退情況
當鍵盤敲下后退鍵(Backspace)后怎么防止網(wǎng)頁后退情況呢?今天小編通過本文給大家詳細介紹下,感興趣的朋友一起看看吧2016-11-11Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例
這篇文章主要介紹了Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02DTO 實現(xiàn) service 和 controller 之間值傳遞的操作
這篇文章主要介紹了DTO 實現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02javaweb Servlet開發(fā)總結(jié)(二)
這篇文章主要為大家詳細介紹了javaweb Servlet開發(fā)總結(jié)的第二篇,感興趣的小伙伴們可以參考一下2016-05-05