Spring 配置解析之Properties

来源:http://www.cnblogs.com/dragonfei/archive/2016/09/25/5906474.html
-Advertisement-
Play Games

1.簡單示例: SpringBoot中的的配置簡單屬性類支持ConfigurationProperties方式,看一個簡單的示例。 1 @ConfigurationProperties(prefix = "org.dragonfei.demo") 2 public class DemoPropert ...


1.簡單示例:

SpringBoot中的的配置簡單屬性類支持ConfigurationProperties方式,看一個簡單的示例。

 1 @ConfigurationProperties(prefix = "org.dragonfei.demo")
 2 public class DemoProperties {
 3     private String name;
 4     private String password;
 5     private String test;
 6 
 7     public String getName() {
 8         return name;
 9     }
10 
11     public void setName(String name) {
12         this.name = name;
13     }
14 
15     public String getPassword() {
16         return password;
17     }
18 
19     public void setPassword(String password) {
20         this.password = password;
21     }
22 
23     public String getTest() {
24         return test;
25     }
26 
27     public void setTest(String test) {
28         this.test = test;
29     }
30 
31     @Override
32     public String toString() {
33         return "DemoProperties{" +
34                 "name='" + name + '\'' +
35                 ", password='" + password + '\'' +
36                 ", test='" + test + '\'' +
37                 '}';
38     }
39 }
定義Properties類
1 org.dragonfei.demo.name=dragonfei
2 org.dragonfei.demo.password=password
3 org.dragonfei.demo.test=test
添加配置
1 @Configuration
2 @EnableConfigurationProperties({DemoProperties.class})
3 public class DemoConfiguration {
4 }
註入Properties
 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @SpringApplicationConfiguration(classes = DemoConfiguration.class)
 3 @EnableAutoConfiguration
 4 public class DemoPropertiesTest {
 5     
 6     @Autowired
 7     private DemoProperties properties;
 8     @Test
 9     public void testProperties(){
10         System.out.println(properties.toString());
11     }
12 }
簡單單元測試
1 DemoProperties{name='dragonfei', password='password', test='test'}
運行單元測試結果

DemoProperties神奇的註入到Spring容器中了。有沒有跟我一樣很興奮,這樣的 一大好處,將配置文件的屬性以類的形式展現,在需要使用的時候只需要,autowire需要的類就可以了,避免大片重覆的的${a.b.c}

 2.Properties屬性自動裝配實現

DemoProperties這麼神奇註入到容器中,天下沒有什麼是莫名奇妙的,引出了兩個關鍵問題:

  • DemoProperties是怎樣註入到容器中?
  • DemoProperties中的各個屬性是怎麼被賦值的呢?

  要回答上面的問題,必須對@Configuration如何註入bean做一個簡單的回顧:

  • 在解析@Congiguraion的時候,會調用@Import中引入的類
  • 如果@Import中是ImportBeanDefinitionegistar的子類,會直接調用registerBeanDefinitions
  • 如果@Import中是ImportSelector類型,會調用selectImports()返回的bean的registerBeanDefinitions方法。
  • registerBeanDefinitions方法會向BeanFactory中添加新的bean。

回到正題。打開EnableConfigurationProperties

 1 @Target(ElementType.TYPE)
 2 @Retention(RetentionPolicy.RUNTIME)
 3 @Documented
 4 @Import(EnableConfigurationPropertiesImportSelector.class)
 5 public @interface EnableConfigurationProperties {
 6 
 7     /**
 8      * Convenient way to quickly register {@link ConfigurationProperties} annotated beans
 9      * with Spring. Standard Spring Beans will also be scanned regardless of this value.
10      * @return {@link ConfigurationProperties} annotated beans to register
11      */
12     Class<?>[] value() default {};
13 
14 }
View Code

註意@Imoport裡面的類

 1     public String[] selectImports(AnnotationMetadata metadata) {
 2         MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
 3                 EnableConfigurationProperties.class.getName(), false);
 4         Object[] type = attributes == null ? null
 5                 : (Object[]) attributes.getFirst("value");
 6         if (type == null || type.length == 0) {
 7             return new String[] {
 8                     ConfigurationPropertiesBindingPostProcessorRegistrar.class
 9                             .getName() };
10         }
11         return new String[] { ConfigurationPropertiesBeanRegistrar.class.getName(),
12                 ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
13     }
View Code

然後,會調用ConfigurationPropertiesBeanRegistar和ConfigurationPropertiesBindingPostProcessorRegistar的registerBeanDefinitions方法,前者是為了註入配置properties類,後者為屬性綁定值

 1     @Override
 2         public void registerBeanDefinitions(AnnotationMetadata metadata,
 3                 BeanDefinitionRegistry registry) {
 4             MultiValueMap<String, Object> attributes = metadata
 5                     .getAllAnnotationAttributes(
 6                             EnableConfigurationProperties.class.getName(), false);
 7             List<Class<?>> types = collectClasses(attributes.get("value"));
 8             for (Class<?> type : types) {
 9                 String prefix = extractPrefix(type);
10                 String name = (StringUtils.hasText(prefix) ? prefix + "-" + type.getName()
11                         : type.getName());
12                 if (!registry.containsBeanDefinition(name)) {
13                     registerBeanDefinition(registry, type, name);
14                 }
15             }
16         }
ConfigurationPropertiesBeanRegistar之registerBeanDefinitions
 1     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
 2             BeanDefinitionRegistry registry) {
 3         if (!registry.containsBeanDefinition(BINDER_BEAN_NAME)) {
 4             BeanDefinitionBuilder meta = BeanDefinitionBuilder
 5                     .genericBeanDefinition(ConfigurationBeanFactoryMetaData.class);
 6             BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(
 7                     ConfigurationPropertiesBindingPostProcessor.class);
 8             bean.addPropertyReference("beanMetaDataStore", METADATA_BEAN_NAME);
 9             registry.registerBeanDefinition(BINDER_BEAN_NAME, bean.getBeanDefinition());
10             registry.registerBeanDefinition(METADATA_BEAN_NAME, meta.getBeanDefinition());
11         }
12     }
ConfigurationPropertiesBindingPostProcessorRegistar之registerBeanDefinition

註意這裡註入了ConfigurationPropertiesBindingPostProcessor,這才是屬性賦值的關鍵。查看類圖

類圖

註意到ConfigurationPropertiesBindingPostProcessor繼承自BeanPostProcessor,他會在bean初始化前後調用before和after後置處理,這裡,在Properties屬性初始化完成後,會對綁定屬性,

 1 private void postProcessBeforeInitialization(Object bean, String beanName,
 2             ConfigurationProperties annotation) {
 3         Object target = bean;
 4         PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
 5                 target);
 6         if (annotation != null && annotation.locations().length != 0) {
 7             factory.setPropertySources(
 8                     loadPropertySources(annotation.locations(), annotation.merge()));
 9         }
10         else {
11             factory.setPropertySources(this.propertySources);
12         }
13         factory.setValidator(determineValidator(bean));
14         // If no explicit conversion service is provided we add one so that (at least)
15         // comma-separated arrays of convertibles can be bound automatically
16         factory.setConversionService(this.conversionService == null
17                 ? getDefaultConversionService() : this.conversionService);
18         if (annotation != null) {
19             factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields());
20             factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
21             factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
22             factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
23             if (StringUtils.hasLength(annotation.prefix())) {
24                 factory.setTargetName(annotation.prefix());
25             }
26         }
27         try {
28             factory.bindPropertiesToTarget();
29         }
30         catch (Exception ex) {
31             String targetClass = ClassUtils.getShortName(target.getClass());
32             throw new BeanCreationException(beanName, "Could not bind properties to "
33                     + targetClass + " (" + getAnnotationDetails(annotation) + ")", ex);
34         }
35     }
綁定屬性

 至於真實的數據綁定,會從propertySources中獲取,敬請期待....Spring 的數據綁定,這簡單提一下關鍵的地方:

1 Set<String> names = getNames(relaxedTargetNames);
2         PropertyValues propertyValues = getPropertyValues(names, relaxedTargetNames);
View Code

請註意getNames,是獲取prefix+屬性構成的key值,prefix_property和prefix.property都會獲取到

 1     private Set<String> getNames(Iterable<String> prefixes) {
 2         Set<String> names = new LinkedHashSet<String>();
 3         if (this.target != null) {
 4             PropertyDescriptor[] descriptors = BeanUtils
 5                     .getPropertyDescriptors(this.target.getClass());
 6             for (PropertyDescriptor descriptor : descriptors) {
 7                 String name = descriptor.getName();
 8                 if (!name.equals("class")) {
 9                     RelaxedNames relaxedNames = RelaxedNames.forCamelCase(name);
10                     if (prefixes == null) {
11                         for (String relaxedName : relaxedNames) {
12                             names.add(relaxedName);
13                         }
14                     }
15                     else {
16                         for (String prefix : prefixes) {
17                             for (String relaxedName : relaxedNames) {
18                                 names.add(prefix + "." + relaxedName);
19                                 names.add(prefix + "_" + relaxedName);
20                             }
21                         }
22                     }
23                 }
24             }
25         }
26         return names;
27     }
View Code

getPropertyValues會獲取到滿足上述條件的propertyValues,最後調用spring框架提供數據綁定策略進行數據綁定。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • datagridview編輯列時,對於類中類的屬性綁定,嘗試了一下”類名.屬性名“ 並沒有正確綁定,後又嘗試了幾次無果,只能利用Linq來解決了, 首先,datagridview要綁定的是學生類(Student),而學生類里有老師類(Teacher) 正常是這樣:datagridview.Datas ...
  • 內置對象:用於頁面之間的數據交互 為什麼要使用這麼內置對象?因為HTTP的無狀態性。 Session:在電腦中,尤其是在網路應用中,稱為“會話控制”。Session 對象存儲特定用戶會話所需的屬性及配置信息。這樣,當用戶在應用程式的 Web 頁之間跳轉時,存儲在 Session 對象中的變數將不會 ...
  • Session:在不同的瀏覽器之間傳值,像銀行之類的網站為了安全把用戶名密碼保存在session裡面。每一臺電腦訪問伺服器,都會是獨立的一套session,key值都一樣,但是內容都是不一樣的 以上所有內容,都跟cookies一樣 不一樣:1、Session的保存位置是保存在伺服器上2、Sessio ...
  • 一、深入.NET框架 1..NET框架具有兩個組件:CLR(公共語言運行時)和FCL(框架類庫),CLR是.NET框架的基礎 2.框架核心類庫: System.Collections.Generic:泛型操作 System.IO:IO流操作 System.Net:網路編程 System.Data:A ...
  • ASP.NET HTTP 處理程式是響應對 ASP.NET Web 應用程式的請求而運行的過程(通常稱為"終結點")。最常用的處理程式是處理 .aspx 文件的 ASP.NET 頁處理程式。用戶請求 .aspx 文件時,頁通過頁處理程式來處理請求。 ASP.NET 頁處理程式僅僅是一種類型的處理程式 ...
  • 硬碟和記憶體的作用是什麼 硬碟的作用毫無疑問我們大家都清楚,不就是用來存儲數據文件的麽?如照片、視頻、各種文檔或等等,肯定也有你喜歡的某位島國老師的動作片,這個時候無論我們電腦是否關機重啟它們永遠在那裡,不會無辜地消失掉。那記憶體是用來做什麼的呢?我是不能準確的描述出來,所以我抄襲了下麵描述記憶體作用的一 ...
  • 1、首先他們兩個介面都是為了實現對象的序列化,使之可以傳遞,所謂序列化就是將對象信息裝換成可以存儲的介質的過程。 2、Serializable是jdk所提供的序列化介面,該介面存在於io包下,可想用於輸入輸出,使用非常簡單,只要讓你的類實現此介面就ok了;可以使用transient關鍵字修飾你不想序 ...
  • 約定:句子以空格為詞語分割符號,以句號為結束符號。 實現思路: 用函數explode(separator,string,limit)對字元串進行分割,再對得到的數據最後一個成員分割切掉符號。用一個新的數組倒序接收轉為字元串,並補上句號。 代碼實現: 效果: ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...