栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring之PropertyResourceConfigurer及其子类详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring之PropertyResourceConfigurer及其子类详解

一、PropertyResourceConfigurer类

package org.springframework.beans.factory.config;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.io.support.PropertiesLoaderSupport;
import org.springframework.util.ObjectUtils;


public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport
		implements BeanFactoryPostProcessor, PriorityOrdered {

	private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered


	
	public void setOrder(int order) {
		this.order = order;
	}

	@Override
	public int getOrder() {
		return this.order;
	}


	
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		try {
			Properties mergedProps = mergeProperties();

			// Convert the merged properties, if necessary.
			convertProperties(mergedProps);

			// Let the subclass process the properties.
			processProperties(beanFactory, mergedProps);
		}
		catch (IOException ex) {
			throw new BeanInitializationException("Could not load properties", ex);
		}
	}

	
	protected void convertProperties(Properties props) {
		Enumeration propertyNames = props.propertyNames();
		while (propertyNames.hasMoreElements()) {
			String propertyName = (String) propertyNames.nextElement();
			String propertyValue = props.getProperty(propertyName);
			String convertedValue = convertProperty(propertyName, propertyValue);
			if (!ObjectUtils.nullSafeEquals(propertyValue, convertedValue)) {
				props.setProperty(propertyName, convertedValue);
			}
		}
	}

	
	protected String convertProperty(String propertyName, String propertyValue) {
		return convertPropertyValue(propertyValue);
	}

	
	protected String convertPropertyValue(String originalValue) {
		return originalValue;
	}


	
	protected abstract void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
			throws BeansException;

}

翻译注释:

Allows for configuration of individual bean property values from a property resource, i.e. a properties file. Useful for custom config files targeted at system administrators that override bean properties configured in the application context.
允许从属性资源(即属性文件)配置单个bean属性值。对于覆盖在应用程序上下文中配置的bean属性的针对系统管理员的自定义配置文件非常有用。

Two concrete implementations are provided in the distribution:
PropertyOverrideConfigurer for “beanName.property=value” style overriding (pushing values from a properties file into bean definitions)
PropertyPlaceholderConfigurer for replacing “${…}” placeholders (pulling values from a properties file into bean definitions)
发行版中提供了两个具体实现:

“beanName.property=value”样式重写的PropertyOverrideConfigurer(将属性文件中的值推送到bean定义中)

用于替换“${…}”占位符的PropertyPlaceHolderConfigure(将属性文件中的值拉入bean定义)

Property values can be converted after reading them in, through overriding the convertPropertyValue method. For example, encrypted values can be detected and decrypted accordingly before processing them.
在读入属性值后,可以通过重写convertPropertyValue方法来转换属性值。例如,可以在处理加密值之前相应地检测并解密它们。

由上面的注释可知,这个类主要由两个子类PropertyOverrideConfigurer和PropertyPlaceHolderConfigure,主要是将配置文件中的属性赋值给Spring容器中的bean中。我们连接数据库的配置信息,包括${}替换properties中的属性,都是通过这个类及其子类实现的,下面我们看源码研究其实现原理。

首先可以看到,他是一个抽象类,继承了PropertiesLoaderSupport类,实现了BeanFactoryPostProcessor和PriorityOrdered接口。

这里我们先知道PropertyResourceConfigurer类实现了BeanFactoryPostProcessor类,即在形成BeanDefiniton,实例化之前,将properties中的属性进行了替换。

可以看出这几个类都是实现了排序和优先级接口的,所以,它的运行流程就是先解析properties中的key和value。将其存入内存,然后再解析xml中配置的 , 将 p r o p e r t i e s 解 析 的 值 , 替 换 到 {},将properties解析的值,替换到 ,将properties解析的值,替换到{}中去。这几个类就做了这点儿事情。所以我们在配置数据库连接时好用这种配置,其原理就出自于此。

注意:有些项目里面,我们可以看到数据库配置的密码是加密的,那就说明数据库密码是加密的吗?未必,我们有时候为了不让开发人员知道数据库的密码,可以在配置数据库时,对密码加密,而利用PropertyResourceConfigurer类,读取配置文件的信息,再对其进行解密,这样可以实现需求。PropertyResourceConfigurer类提供了解密的方法,如下:

	
	protected String convertProperty(String propertyName, String propertyValue) {
		return convertPropertyValue(propertyValue);
	}

	
	protected String convertPropertyValue(String originalValue) {
		return originalValue;
	}

PropertyResourceConfigurer是个抽象类,上面的两个方法,原值返回了,我们可以继承这个类,然后对值进行一下解密,然后返回揭秘后的值。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306536.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号