spring-扩展点-BeanFactoryPostProcessor

BeanFactoryPostProcessor:允许自定义对ApplicationContext的 bean definitions 进行修饰,扩展功能。

1、实现BeanFactoryPostProcessor 接口,会被Application contexts自动发现
2、BeanFactoryPostProcessor 仅仅对 bean definitions 发生关系,不能对bean instances 交互,对bean instances 的交互,由BeanPostProcessor的实现来处理
3、PropertyResourceConfigurer :典型实现 属性替换的功能

4、CustomEditorConfigurer :典型实现 添加自定义属性编辑器的功能
5、ConfigurationClassPostProcessor:启动@Configuration的扫描功能
举例:
有这样的也个业务场景:

 <bean id="user" class="com.gym.UserServiceImpl" >
       <property name="username" value="${username_}"/>
       <property name="password" value="${password_}"/>
</bean>



spring支持系统对username_进行占位符的配置为properties文件配置,试想如果我们有个配置中心,我们希望spring启动的时候,从远程配置中心取数据,而非本地文件,这里就需要我们自定义一个实现BeanFactoryPostProcessor的PropertyResourceConfigurer 实例。
看下面的例子:

xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-autowire="byName">

     <bean id="user" class="com.gym.UserServiceImpl" >
       <property name="username" value="${username_}"/>
       <property name="password" value="${password_}"/>
     </bean>
     
     <bean id="myFactoryPostProcessor" class="com.gym.MyFilePlaceHolderBeanFactoryPostProcessor"/>
</beans>



模拟从远程取文件:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * @author xinchun.wang
 */
public class MyFilePlaceHolderBeanFactoryPostProcessor 
    extends PropertyPlaceholderConfigurer implements InitializingBean{
	
	public void afterPropertiesSet() throws Exception {
		List<Properties> list = new ArrayList<Properties>();
		Properties p = PropertiesLoaderUtils.loadAllProperties("config.properties");
		list.add(p);
		//这里是关键,这就设置了我们远程取得的List<Properties>列表
		setPropertiesArray(list.toArray(new Properties[list.size()]));
	}
	
}



javaBean配置:

public class UserServiceImpl implements IUserService{
	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	public UserServiceImpl(){
		logger.info("UserServiceImpl 构造函数 ");
	}
	
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public void setUsername(String username) {
		logger.info("UserServiceImpl setUsername {}",username);
		this.username = username;
	}

	public void setPassword(String password) {
		logger.info("UserServiceImpl setPassword {}",password);
		this.password = password;
	}

	@Override
	public String toString() {
		return "UserServiceImpl [username=" + username + ", password="
				+ password + "]";
	}
	
}



测试:

public class TestApplicationContext {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"classpath:spring/applicationContext.xml");
		IUserService userService = applicationContext.getBean(IUserService.class);
		String password = userService.getPassword();
		applicationContext.destroy();
		System.out.println(password);
	}

}



------------------------------系统调用入口:------------------------------
调用入口:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值