`
8821249
  • 浏览: 67338 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

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

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

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

四、特殊的后处理Bean实现
  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>

ioc.Address为要使用属性编辑器的类,ioc.AddressEditor为使用的属性编辑器

  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>

ioc/address.properties是属性文件所在的位置

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics