博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-全量自定义-PropertySource
阅读量:4144 次
发布时间:2019-05-25

本文共 1923 字,大约阅读时间需要 6 分钟。

PropertySource:属性资源,以name/value形式存储

protected final String name; //属性名字protected final T source; //属性资源

配合属性解析器使用的接口:

public boolean containsProperty(String name) {//判断该资源是否含有指定名称的key		return (getProperty(name) != null);	}@Nullablepublic abstract Object getProperty(String name);//从资源中获取值

占位资源,用于保证自己的顺序,后续通过replace换成真实资源,例如ServeltContext等:

public static class StubPropertySource extends PropertySource {		public StubPropertySource(String name) {			super(name, new Object());		}		@Override		@Nullable		public String getProperty(String name) {			return null;		}	}

在springframework核心包中有一个实现,可以枚举属性名字:

public abstract class EnumerablePropertySource
extends PropertySource
{ public EnumerablePropertySource(String name, T source) { super(name, source); } protected EnumerablePropertySource(String name) { super(name); } @Override public boolean containsProperty(String name) { return ObjectUtils.containsElement(getPropertyNames(), name); } //枚举所有属性名字 public abstract String[] getPropertyNames();}

EnumerablePropertySource的直接实现:

CompositePropertySource (org.springframework.core.env)//符合型实现,有一个set持有PropertySourceCommandLinePropertySource (org.springframework.core.env)//命令行属性资源MapPropertySource (org.springframework.core.env)//map型资源AnnotationsPropertySource (org.springframework.boot.test.autoconfigure.properties)//通过注解注入的属性资源

其中MapPropertySource是使用较广的,他的实现有:

PropertiesPropertySource (org.springframework.core.env)//通过Properties资源构建属性资源       ResourcePropertySource (org.springframework.core.io.support)//直接通过资源路径构建属性资源       MockPropertySource (org.springframework.mock.env)//测试使用的属性资源SystemEnvironmentPropertySource (org.springframework.core.env)//对系统环境转换成属性资源

命令行属性资源:

CommandLinePropertySource (org.springframework.core.env)		SimpleCommandLinePropertySource (org.springframework.core.env)//能够吧命令行参数封装成属性资源

通过上面的源码解析,可以看到,spring的属性资源,按照不同的场景,可以自行构建自定义属性资源,从而能解析符合自己业务需求的资源,比如从某个网络位置进行资源解析,等等.

转载地址:http://wduti.baihongyu.com/

你可能感兴趣的文章
Ubuntu下安装Qt
查看>>
Qt札记
查看>>
我的vimrc和gvimrc配置
查看>>
hdu 4280
查看>>
禁止使用类的copy构造函数和赋值操作符
查看>>
C++学习路线
查看>>
私有构造函数
查看>>
组队总结
查看>>
TitledBorder 设置JPanel边框
查看>>
DBCP——开源组件 的使用
查看>>
抓包工具
查看>>
海量数据相似度计算之simhash和海明距离
查看>>
DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
查看>>
DeepLearning tutorial(6)易用的深度学习框架Keras简介
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
流形学习-高维数据的降维与可视化
查看>>
Python-OpenCV人脸检测(代码)
查看>>
python+opencv之视频人脸识别
查看>>
人脸识别(OpenCV+Python)
查看>>
6个强大的AngularJS扩展应用
查看>>