`
lxz891117
  • 浏览: 33337 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

 
阅读更多


可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。

从上图中,我们看到PropertyPlaceholderConfigurer实现了三个bean生命周期的接口:BeanFactoryAware & BeanNameAware & BeanFactoryPostProcessor。关于spring bean的生命周期,可以参考这里http://blog.csdn.net/gjb724332682/article/details/46767463

PropertyResourceConfigurer.postProcessBeanFactory()将properties文件中的属性进行merge,convert,最后调用PropertyPlaceholderConfigurer.processProperties()完成遍历bean定义替换属性占位符。


例子:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/conf/xx.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>


<!--当然也可以引入多个属性文件,如: -->


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath:conf/sqlmap/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="econsoleDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>


除此之外,我们还可以扩展自这个类,用来诸如加解密配置信息等操作。如下:

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.xxx.util.AESUtils;

public class DecryptPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {
	private String key = "xxxxxx";

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactory, Properties props)
			throws BeansException {
		try {
			String driverClassName = props.getProperty("driverClassName");
			if (driverClassName != null) {
				props.setProperty("driverClassName",
						AESUtils.aesDecrypt(driverClassName, key));
			}

			String url = props.getProperty("url");
			if (url != null) {
				props.setProperty("url", AESUtils.aesDecrypt(url, key));
			}

			String username = props.getProperty("username");
			if (username != null) {
				props.setProperty("username",
						AESUtils.aesDecrypt(username, key));
			}

			String password = props.getProperty("password");
			if (password != null) {
				props.setProperty("password",
						AESUtils.aesDecrypt(password, key));
			}
			super.processProperties(beanFactory, props);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BeanInitializationException(e.getMessage());
		}
	}
}
重写processProperties方法就可以。

分享到:
评论

相关推荐

    Spring项目中怎么配置log4j

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:log4j.properties"/&gt; ``` 这使得Spring在启动时...

    Spring中PropertyPlaceholderConfigurer的使用

    &lt;bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 在上面的代码中,...

    ssh框架在application.xml中配置数据源所需jar

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/deploy.properties &lt;!-- 配置sessionFactory &lt;bean id="sessionFactory" class="...

    SPRING:bean配置properties

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 这里,`propertyConfigurerForAnalysis`是`...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:proxool.properties &lt;bean id="dataSource" class="org.logicalcobwebs.proxool....

    Spring项目application.xml配置文件加解密

    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.spring...

    ssm框架整合

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:database.properties &lt;bean id="dataSource" class="org.springframework.jdbc.datasource....

    springmvc+spring+mybatis

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置数据源 --&gt; &lt;bean id="dataSource" class="org.apache....

    struts2 hibernate spring集成

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 使用proxool连接池配置数据源 --&gt; class="org.logicalcobwebs.proxool....

    spring 配置

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="/WEB-INF/config/jdbc.properties"/&gt; ``` 然后,配置数据源`dataSource`,这里使用了C3P0...

    关于spring系统中多系统的配置

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 这里的`location`属性指定了属性文件的位置。`classpath:`...

    Spring属性占位符PropertyPlaceholderConfigurer的使用

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:config.properties"/&gt; ``` 在上面的代码中,`...

    spring3.2+strut2+hibernate4

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath*:jdbc.properties &lt;!-- 数据源配置,主要用于开发测试...

    SSI框架整合实例

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt; ${...

    对SAE主从数据库连接的管理和封装.docx

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:db.properties &lt;!-- 主数据库配置 --&gt; ...

Global site tag (gtag.js) - Google Analytics