Spring中PropertiesLoaderSupport源码简介说明
下文笔者讲述PropertiesLoaderSupport源码简介说明,如下所示
PropertiesLoaderSupport简介说明
Spring中PropertiesLoaderSupport是一个抽象基类 此抽象类供其他子类使用 =============================================== PropertiesLoaderSupport实现子类有PropertiesFactoryBean,PropertyResourceConfigurer
PropertiesLoaderSupport
PropertiesLoaderSupport位于包 org.springframework.core.io.support 中
PropertiesLoaderSupport中相关属性
// 本地属性,通过设置Properties对象直接设置进来的属性
@Nullable
protected Properties[] localProperties;
// 本地属性和外来属性之间的优先级,或者叫做覆盖关系
// false 表示 外来属性优先级高于本地属性 (缺省值)
// true 表示 本地属性优先级高于外来属性
protected boolean localOverride = false;
// 外来属性对应资源,通过设置外部资源位置设置进来需要加载的属性
@Nullable
private Resource[] locations;
// 读取外来属性时遇到不存在的资源路径应该怎么办 ?
// false : 输出一个日志,然后继续执行其他逻辑 (缺省值)
// true : 抛出异常
private boolean ignoreResourceNotFound = false;
// 加载外来属性资源文件时使用的字符集
@Nullable
private String fileEncoding;
// 外来属性加载工具
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
mergeProperties
* Return a merged Properties instance containing both the
* loaded properties and properties set on this FactoryBean.
*
*/
protected Properties mergeProperties() throws IOException {
Properties result = new Properties();
if (this.localOverride) {
// Load properties from file upfront, to let local properties override.
// localOverride == true, 先加载外来属性到结果对象
loadProperties(result);
}
if (this.localProperties != null) {
// 将本地属性合并到结果对象
for (Properties localProp : this.localProperties) {
CollectionUtils.mergePropertiesIntoMap(localProp, result);
}
}
if (!this.localOverride) {
// Load properties from file afterwards, to let those properties override.
// localOverride == false, 后加载外来属性到结果对象
loadProperties(result);
}
return result;
}
loadProperties方法
/**
* Load properties into the given instance.
* @param props the Properties instance to load into
* @throws IOException in case of I/O errors
* @see #setLocations
*/
protected void loadProperties(Properties props) throws IOException {
if (this.locations != null) {
// 读取每一个属性文件资源
for (Resource location : this.locations) {
if (logger.isTraceEnabled()) {
logger.trace("Loading properties file from " + location);
}
try {
// 使用指定的字符集fileEncoding从外部资源路径location读取属性到props,使用的属性读取工具
// 是 propertiesPersister
PropertiesLoaderUtils.fillProperties(
props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
}
catch (FileNotFoundException | UnknownHostException ex) {
// 出现异常时,如果ignoreResourceNotFound==true,则仅仅记录日志,继续读取下一个
// 资源文件,否则直接抛出该异常
if (this.ignoreResourceNotFound) {
if (logger.isDebugEnabled()) {
logger.debug("Properties resource not found: " + ex.getMessage());
}
}
else {
throw ex;
}
}
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


