---
title: "Spring PropertySource 学习"
author: "baitian"
date: "2012-12-02T13:10:44Z"
tags: ["Spring"]
categories: ["企业架构"]
slug: "spring-propertysource"
draft: false
lastmod: "2012-12-02T13:10:44Z"
id: 1739679
url: "https://baitian.iteye.com/blog/1739679"
---

PropertySource 是PropertySources 容器唯一可存储的元素。
PropertySources  定义了一个借口：
1：根据key，得到value
2：遍历整个容器
PropertySources 在SPRING 中的唯一实现是MutablePropertySources,
MutablePropertySources 不仅实现借口定义的两个功能，还增加了可以插入，删除，替代的功能，
并且，插入是有优先级概念的，这意味着PropertySource 在PropertySources中的插入顺序不是固定的，
因此实际是MutablePropertySources内部采用的数据结构是链表形式的LinkedList

虽然容器PropertySources提供了基本的查找功能.但是查找效率不高：

```java
	
public PropertySource<?> get(String name) {
		return this.propertySourceList.get(this.propertySourceList.indexOf(PropertySource.named(name)));
	}

```

首先新建一个ComparsionPropertySource对象：PropertySource.named(name)
然后遍历这个List，找出该对象的位置，
最后再一次遍历LIST，返回对应位置的PropertySources。

Spring 又提供一个专门用来查找Property的接口 PropertyResolver。
这个接口最重要的是提供了property value 的类型转换接口。
该接口的一个实现PropertySourcesPropertyResolver ，重新实现了在PropertySources中查找PropertySource的接口，增加了对PlaceHolder的解析的接口。

PropertyResolver 的主要目的就是查找配置（.properties，环境变量）
PropertySource 则用途更全面。
奇怪的是PropertySource 被设计为abstract class，而PropertyResolver 
被设计成interface。