1. 程式人生 > >【Spring註解驅動開發】使用@PropertySource載入配置檔案,我只看這一篇!!

【Spring註解驅動開發】使用@PropertySource載入配置檔案,我只看這一篇!!

## 寫在前面 > 很多小夥伴都在問:冰河,你的Spring專題更新完了嗎?怎麼感覺像是寫了一半啊?我:沒有更新完呀,整個專題預計會有70多篇。那怎麼更新了一半就去寫別的了呢?那是因為有很多其他的小夥伴在後臺留言說:急需學習一些其他的技術,所以,臨時調整的。放心,Spring專題會持續更新的!這不,今天,我們就繼續更新Spring專題。不出意外的話,會一直持續更新完!! > > 專案工程原始碼已經提交到GitHub:[https://github.com/sunshinelyz/spring-annotation](https://github.com/sunshinelyz/spring-annotation) ## @PropertySource註解概述 @PropertySource註解是Spring 3.1開始引入的配置類註解。通過@PropertySource註解將properties配置檔案中的值儲存到Spring的 Environment中,Environment介面提供方法去讀取配置檔案中的值,引數是properties檔案中定義的key值。也可以使用@Value 註解用${}佔位符注入屬性。 @PropertySource註解的原始碼如下所示。 ```java package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.io.support.PropertySourceFactory; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(PropertySources.class) public @interface PropertySource { String name() default ""; String[] value(); boolean ignoreResourceNotFound() default false; String encoding() default ""; Class factory() default PropertySourceFactory.class; } ``` 從@PropertySource的原始碼可以看出,我們可以通過@PropertySource註解指定多個properties檔案,可以使用如下形式進行指定。 ```java @PropertySource(value={"classpath:xxx.properties", "classpath:yyy.properties"}) ``` 細心的讀者可以看到,在@PropertySource註解類的上面標註瞭如下的註解資訊。 ```java @Repeatable(PropertySources.class) ``` 看到這裡,小夥伴們是不是有種恍然大悟的感覺呢?沒錯,我們也可以使用@PropertySources註解來指定properties配置檔案。 ## @PropertySources註解 首先,我們也是看下@PropertySources註解的原始碼,如下所示。 ```java package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PropertySources { PropertySource[] value(); } ``` @PropertySources註解的原始碼比較簡單,只有一個PropertySource[]陣列型別的屬性value,那我們如何使用@PropertySources註解指定配置檔案呢?其實也很簡單,就是使用如下所示的方式就可以了。 ```java @PropertySources(value={ @PropertySource(value={"classpath:xxx.properties"}), @PropertySource(value={"classpath:yyy.properties"}), }) ``` 是不是很簡單呢?接下來,我們就以一個小案例來說明@PropertySource註解的用法。 ## 案例準備 首先,我們在工程的src/main/resources目錄下建立一個配置檔案person.properties檔案,檔案的內容如下所示。 ```bash person.nickName=zhangsan ``` 接下來,我們在Person類中新增一個欄位nickName,如下所示。 ```java package io.mykit.spring.plugins.register.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import org.springframework.beans.factory.annotation.Value; import java.io.Serializable; /** * @author binghe * @version 1.0.0 * @description 測試實體類 */ @Data @ToString @NoArgsConstructor @AllArgsConstructor public class Person implements Serializable { private static final long serialVersionUID = 7387479910468805194L; @Value("binghe") private String name; @Value("#{20-2}") private Integer age; private String nickName; } ``` 目前,我們並沒有為Person類的nickName欄位賦值,所以,此時Person類的nickName欄位的值為空。我們執行下PropertyValueTest類的testPropertyValue01()方法來看下輸出結果,如下所示。 ```bash org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory propertyValueConfig person ================================ Person(name=binghe, age=18, nickName=null) Process finished with exit code 0 ``` 可以看出,Person類的nickName欄位的值確實輸出了null。 ## 使用xml檔案方式獲取值 如果我們需要在xml檔案中獲取person.properties檔案中的值,則我們首先需要在Spring的xml檔案中引入context名稱空間,並且使用context名稱空間匯入person.properties檔案,之後在bean的屬性欄位中使用如下方式將person.properties檔案中的值注入到Person類的nickName欄位上。