spring4.3.6源代码 - beans - 属性值使用占位符[PropertyPlaceholderConfigurer]

37 篇文章 0 订阅
21 篇文章 0 订阅

 

1、编写配置文件

# classpath:/cn/java/demo/beantag/placeholder0.properties


# beanPlaceholderTest0 \u7684\u52A8\u6001\u914D\u7F6E
beanPlaceholderTest0.factoryMethod=getCreatedByFactoryBean
beanPlaceholderTest0.scope=prototype
beanPlaceholderTest0.constructorArg0=1
beanPlaceholderTest0.constructorArg1=username1_beanPlaceholderTest0
beanPlaceholderTest0.property1=property1_beanPlaceholderTest0

# beanPlaceholderTest1 \u7684\u52A8\u6001\u914D\u7F6E
beanPlaceholderTest1.parent=standardBean0
beanPlaceholderTest1.class=cn.java.demo.beantag.bean.StandardBean
beanPlaceholderTest1.scope=prototype
beanPlaceholderTest1.constructorArg0=1
beanPlaceholderTest1.beanName=beanName_beanPlaceholderTest1_hi

2、注册配置文件到PropertyPlaceholderConfigurer

<!-- 替换BeanDefinition中的占位符  -->
<!-- 
	这个是一个实现org.springframework.beans.factory.config.BeanFactoryPostProcessor接口的bean,
	机制:在beanFactory解析完所有的BeanDefinition后,本bean会扫描所有的BeanDefinition,替换BeanDefinition中的占位符(从properties文件中查找值) 
-->
<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="order" value="0" />
	<property name="locations">
		<list>
		    <value>classpath:/cn/java/demo/beantag/placeholder0.properties</value>
		</list>
	</property>
</bean>

 

3、使用占位符

<bean id="beanPlaceholderTest0"  factory-bean="factoryBean0" factory-method="${beanPlaceholderTest0.factoryMethod}" scope="${beanPlaceholderTest0.scope}">
	<constructor-arg index="0" type="java.lang.Integer" name="userid"  value="${beanPlaceholderTest0.constructorArg0}"/>
	<constructor-arg index="1" type="java.lang.String"  name="username"  value="${beanPlaceholderTest0.constructorArg1}"/>
	<property name="property1" value="${beanPlaceholderTest0.property1}" />
</bean>
<bean id="beanPlaceholderTest1"  parent="${beanPlaceholderTest1.parent}" class="${beanPlaceholderTest1.class}" scope="${beanPlaceholderTest1.scope}">
	<constructor-arg index="0" type="java.lang.Integer"  name="beanId" value="${beanPlaceholderTest1.constructorArg0}" />
	<property name="beanName" value="${beanPlaceholderTest1.beanName}" />
</bean>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值