Spring讀取配置文件屬性實現(xiàn)方法
一 前言
本篇內(nèi)容包括spring 運行時讀取配置文件的多種方式和SpEl表達式入門基礎(chǔ);
二運行時讀取配置文件
spring 運行時讀取配置文件值提供了2種方式
屬性占位符(Property placeholder)。
Spring表達式語言(SpEL)
2.1 讀取外部配置文件
使用 @PropertySource 注解可以讀取導(dǎo)classpath下配置文件屬性;參數(shù)如下
value是個字符串?dāng)?shù)組; ignoreResourceNotFound;如果設(shè)置為true, 配置文件未找到時不會報錯; encoding;指定字符集首先resource 目錄下創(chuàng)建配置文件zszxz.properties ; 內(nèi)容如下
zszxz.name = zszxzzszxz.point = share
其次讀取配置文件配置類如下
@Configuration@PropertySource(value = {'classpath:zszxz.properties'},encoding = 'UTF-8')@Componentpublic class EnvironmentProperty { // 注入環(huán)境 @Autowired private Environment environment; public void outputProperty(){ System.out.println(environment.getProperty('zszxz.name')); }}
最后通過測試類調(diào)用outputProperty()輸出配置文件中屬性的值
@RunWith(SpringJUnit4ClassRunner.class)//創(chuàng)建spring應(yīng)用上下文@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類public class PropertyTest { @Autowired EnvironmentProperty environmentProperty; @Test public void test(){ // zszxz environmentProperty.outputProperty(); }}
Tip 也可以使用@PropertySources 注解,其value是 @PropertySource類型的數(shù)組;
其中 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)//創(chuàng)建spring應(yīng)用上下文@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屬性或者構(gòu)造器參數(shù)中,此過程中能夠計算表達式獲取計算值;使用@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ài)方法示例
其中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); }
支持運算符如下
算術(shù)運算 + 、 - 、 * 、 / 、 % 、 ^ 比較運算 < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge 邏輯運算 and 、 or 、 not 、 │ 條件運算 ?: (ternary) 、 ?: (Elvis) 正則表達式 matches更多內(nèi)容讀者自行參考官網(wǎng)學(xué)習(xí)
https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/expressions.html
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IDEA EasyCode 一鍵幫你生成所需代碼2. IntelliJ IDEA設(shè)置條件斷點的方法步驟3. Java構(gòu)建JDBC應(yīng)用程序的實例操作4. Spring應(yīng)用拋出NoUniqueBeanDefinitionException異常的解決方案5. ThinkPHP5 通過ajax插入圖片并實時顯示(完整代碼)6. javascript設(shè)計模式 ? 建造者模式原理與應(yīng)用實例分析7. 一篇文章帶你了解JavaScript-對象8. Python使用oslo.vmware管理ESXI虛擬機的示例參考9. Express 框架中使用 EJS 模板引擎并結(jié)合 silly-datetime 庫進行日期格式化的實現(xiàn)方法10. 使用AJAX(包含正則表達式)驗證用戶登錄的步驟
