亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring讀取配置文件屬性實現方法

瀏覽:103日期:2023-09-09 16:56:43

一 前言

本篇內容包括spring 運行時讀取配置文件的多種方式和SpEl表達式入門基礎;

二運行時讀取配置文件

spring 運行時讀取配置文件值提供了2種方式

屬性占位符(Property placeholder)。

Spring表達式語言(SpEL)

2.1 讀取外部配置文件

使用 @PropertySource 注解可以讀取導classpath下配置文件屬性;參數如下

value是個字符串數組; ignoreResourceNotFound;如果設置為true, 配置文件未找到時不會報錯; encoding;指定字符集

首先resource 目錄下創建配置文件zszxz.properties ; 內容如下

zszxz.name = zszxzzszxz.point = share

其次讀取配置文件配置類如下

@Configuration@PropertySource(value = {'classpath:zszxz.properties'},encoding = 'UTF-8')@Componentpublic class EnvironmentProperty { // 注入環境 @Autowired private Environment environment; public void outputProperty(){ System.out.println(environment.getProperty('zszxz.name')); }}

最后通過測試類調用outputProperty()輸出配置文件中屬性的值

@RunWith(SpringJUnit4ClassRunner.class)//創建spring應用上下文@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類public class PropertyTest { @Autowired EnvironmentProperty environmentProperty; @Test public void test(){ // zszxz environmentProperty.outputProperty(); }}

Tip 也可以使用@PropertySources 注解,其value是 @PropertySource類型的數組;

其中 EnvironmentProperty 獲取主要屬性方法如下

String getProperty(String key); 通過key 取值 String getProperty(String key, String defaultValue); 獲取值,沒有則使用默認值; T getProperty(String key, Class var2); 獲取值,指定返回類型; T getProperty(String key, Class var2, T defaultValue);獲取值,指定返回類型,指定默認值; String getRequiredProperty(String key) ; key必須為非空否則拋出IllegalStateException異常

2.2 使用占位符獲取配置文件

使用注解@Value獲取配置文件屬性值; 其中值使用占位符('${........}')方式;

配置類示例

@Configuration@PropertySource(value = {'classpath:zszxz.properties'},encoding = 'UTF-8')@Componentpublic class EnvironmentProperty { @Value('${zszxz.point}') private String point; public void outputPoint(){ System.out.println(point); }}

測試示例

@RunWith(SpringJUnit4ClassRunner.class)//創建spring應用上下文@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類public class PropertyTest { @Autowired EnvironmentProperty environmentProperty; @Test public void testPoint(){ // share environmentProperty.outputPoint(); }}

2.3 SpEl表達式

Spring表達式語言(Spring Expression Language,SpEL)是一種靈活的表達式語言,能夠以簡潔的方式將值裝配到bean屬性或者構造器參數中,此過程中能夠計算表達式獲取計算值;使用@Valjue注解時,SpEL表達式要放到“#{......}”之中;

獲取bean示例

@Value('#{environmentProperty}') private EnvironmentProperty getBean; @Test public void testBean(){ // com.zszxz.property.EnvironmentProperty$$EnhancerBySpringCGLIB$$8e54e11f@1d9b7cce System.out.println(getBean); }

獲取方法示例

@Value('#{environmentProperty.getStr()}') private String getMethod; @Test public void testMethod(){ // 知識追尋者 System.out.println(getMethod); }

獲取屬性示例

注意點:username字段必須是public

@Value('#{environmentProperty.username}') private String getField; @Test public void testField(){ // 知識追尋者 System.out.println(getField); }

獲取靜態方法示例

其中T()表示運算會得到一個Class對象;

@Value('#{T(java.lang.Math).random()}') private double number; @Test public void testStatic() { // 0.9205474938572363 System.out.println(number); }

非空判定示例

其中? 表示非空判定

@Value('#{environmentProperty.username?.toString()}') private String notNull; @Test public void testNotNUll() { // 知識追尋者 System.out.println(notNull); }

支持運算符如下

算術運算 + 、 - 、 * 、 / 、 % 、 ^ 比較運算 < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge 邏輯運算 and 、 or 、 not 、 │ 條件運算 ?: (ternary) 、 ?: (Elvis) 正則表達式 matches

更多內容讀者自行參考官網學習

https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/expressions.html

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产全黄a一级毛片视频 | 亚洲欧美日韩精品久久亚洲区 | 欧美一级特黄真人毛片 | 免费鲁丝片一级观看 | 国产欧美综合精品一区二区 | 三级黄色毛片 | 在线看黄免费 | 日韩高清在线 | 一级成人a毛片免费播放 | 久久香蕉精品成人 | 免费一级黄 | 国产在线欧美精品 | 中文字幕在线观 | 国产精品永久免费 | 国产肥老妇视频∵ | 欧美变态口味重另类日韩毛片 | 亚洲成熟| 四色婷婷婷婷色婷婷开心网 | 综合网在线 | 亚洲精品网站在线 | 古代级a毛片可以免费看 | 国产成人精品在视频 | 国产精品视频福利 | 午夜一级福利 | 免费看人做人爱视频拍拍拍 | 亚洲高清视频一区 | 97菊爱网| 国产免费福利视频 | 亚洲一区国产 | 一级做a爰片性色毛片中国 一级做a爰片性色毛片男 | 免费看欧美一级特黄a毛片 免费看欧美一级特黄α大片 | 99re九精品视频在线视频 | 欧美日比视频 | 中文日韩字幕一区在线观看 | 国产一级大片 | 播放一级黄色录像 | 亚洲综合三区 | 久久福利青草狠狠午夜 | 国产麻豆入在线观看 | 日韩一区二区视频在线观看 | 又黄又爽又猛大片录像 |