Spring笔记(四)Spring的后处理Bean

[b]一、BeanPostProcessors接口[/b]
1、用于对Bean的功能进行扩展,对Bean进行修改。
2、在Bean初始化操作之前和之后各调用一次。

[b]二、BeanFactoryPostProcessors接口[/b]
1、用于在Bean实例化前,对配置信息进行修改。

[b]三、后处理Bean的使用[/b]
1、先实现接口和方法。
2、在配置文件里注册Bean。
3、Spring会在运行时自动发现有后处理并的实现,并在对应时机调用后处理Bean。

[b]四、特殊的后处理Bean实现[/b]
1、CustomEditorConfigurer属性编辑器。
a、编写类继承PropertyEditorSupport,重写setAsText方法,方法最后要调用setValue方法。
b、在配置文件中声明CustomEditorConfigurer类,将自定义的属性编辑器传入customEditors。
import java.beans.PropertyEditorSupport;

public class AddressEditor extends PropertyEditorSupport {

public void setAsText(String text) throws IllegalArgumentException {
String[] strs = text.split("-");
Address address = new Address();
address.setCity(strs[0]);
address.setStreet(strs[1]);
address.setRoomNum(strs[2]);
setValue(address);
}
}

	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="ioc.Address">
<bean class="ioc.AddressEditor" />
</entry>
</map>
</property>
</bean>

[align=center][i]ioc.Address为要使用属性编辑器的类,ioc.AddressEditor为使用的属性编辑器[/i][/align]
2、PropertyPlaceholderConfigurer读取外部配置文件。
a、写属性文件
b、在Spring配置文件中声明PropertyPlaceholderConfigurer,将属性文件路径传入location属性。
c、使用时,格式类似EL表达式${xxx}。
	<bean id="p" class="ioc.Person">
<property name="addr">
<value>${address.string}</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="ioc/address.properties" />
</bean>

[align=center][i]ioc/address.properties是属性文件所在的位置[/i][/align]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值