Spring Bean后处理器以及容器后处理器

Bean后处理器:即当Spring容器实例化Bean实例之后进行的增强处理。

容器后处理器:对容器本身进行处理,并总是在容器实例化其他任何Bean之前读取配置文件的元数据并可能修改这些数据。


一、Bean后处理器

      实现了BeanPostProcessor接口的类即可作为一个Bean后处理器,以下是一个Bean后处理器的范例

      1、编写一个实现了BeanPostProcessor接口的MyBeanPostProcessor类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package org.meify.core;  
  2.   
  3. import org.meify.bean.AuthorBean;  
  4. import org.springframework.beans.BeansException;  
  5. import org.springframework.beans.factory.config.BeanPostProcessor;  
  6. /** 
  7.  * Bean后处理器 
  8.  * 主要负责对容器初始化其他Bean后进行进一步增强处理 
  9.  * 当Spring容器实例化Bean实例之后,就偶会依次调用Bean后处理器的两个方法对实例Bean进行增强处理。 
  10.  * @description  
  11.  * @version 1.0 
  12.  * @author meify  2014-1-3 下午3:56:39 
  13.  */  
  14. public class MyBeanPostProcessor implements BeanPostProcessor {  
  15.     @Override  
  16.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {  
  17.         // TODO Auto-generated method stub  
  18.         System.out.println(beanName+"初始化之前进行增强处理。。。");  
  19.         return bean;  
  20.     }  
  21.   
  22.     @Override  
  23.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {  
  24.         // TODO Auto-generated method stub  
  25.         System.out.println(beanName+"初始化之后进行增强处理。。。");  
  26.         //重新改编author实例的属性值  
  27.         if(beanName.equals("author")||bean instanceof AuthorBean){  
  28.             AuthorBean author=(AuthorBean) bean;  
  29.             author.setAddress("辽宁省大连市");  
  30.         }  
  31.         return bean;  
  32.     }  
  33. }  

2、在spring配置文件中注册该Bean后处理器

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 注册Bean后处理器 -->  
  2.     <bean id="beanProcessor" class="org.meify.core.MyBeanPostProcessor"/>  


至此一个Bean后处理器即完成了


二、容器后处理器

     同上,容器后处理器实现的是BeanFactoryPostProcessor接口

    1、编写实现了BeanFactoryPostProcessor接口的MyBeanFactoryPostProcessor的容器后处理器

   

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package org.meify.core;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;  
  5. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  6. /** 
  7.  * 容器后处理器 
  8.  * 通常用于对Spring容器进行拓展,并且总是在容器实例化其他任何bean之前读取配置文件的元数据并进行修改 
  9.  * 典型的应用即对数据源的配置,其中url  driver  user passwd等通常配置在properties文件中并使用属性占位符配置器来“填充” 
  10.  * @description  
  11.  * @version 1.0 
  12.  * @author meify  2014-1-3 下午4:31:12 
  13.  */  
  14. public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {  
  15.   
  16.     @Override  
  17.     public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {  
  18.         // TODO Auto-generated method stub  
  19.          System.out.println("对容器进行后处理。。。。");  
  20.     }  
  21.   
  22. }  

2、注册容器后处理器
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 注册容器后处理器 -->  
  2.      <bean id="factoryProcessor" class="org.meify.core.MyBeanFactoryPostProcessor"/>  

这样一个容器后处理器也完成了


最后编写一个测试程序,对以上的两种后处理器进行测试

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package org.meify.test;  
  2.   
  3.   
  4. import org.meify.bean.AuthorBean;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. /** 
  9.  * 获取Spring容器并获取bean实例 
  10.  * 以下代码: 
  11.  * 先获取spring容器,再获取实体bean,将Spring接口与代码耦合在一起,造成代码污染。 
  12.  * @description  
  13.  * @version 1.0 
  14.  * @author meify  2014-1-2 下午2:33:48 
  15.  */  
  16. public class Test01 {  
  17.   
  18.     public static void main(String[] args) {  
  19.         //ApplicationContext的实例即Spring容器,也称之为Spring上下文  
  20.         ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");  
  21.         System.out.println(ctx);  
  22.           
  23.           
  24.           
  25.         AuthorBean author=ctx.getBean("author",AuthorBean.class);  
  26.         //注意,author的初始化时地址为湖北省武穴市,在Bean后处理器中改变为  辽宁省大连市  
  27.         System.out.println("author的地址为:===="+author.getAddress());  
  28.           
  29.           
  30.           
  31.         }  
  32.       
  33. }  

控制台输出如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 2014-1-3 16:33:24 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  2. 信息: Loading XML bean definitions from class path resource [spring-config.xml]  
  3. 对容器进行后处理。。。。  
  4. 2014-1-3 16:33:24 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  5. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c09554: defining beans [book,author,beanProcessor,factoryProcessor]; root of factory hierarchy  
  6. org.springframework.context.support.ClassPathXmlApplicationContext@1cb25f1: startup date [Fri Jan 03 16:33:24 CST 2014]; root of context hierarchy  
  7. author初始化之后进行增强处理。。。  
  8. 正在执行初始化方法。。。  
  9. author初始化之前进行增强处理。。。  
  10. author的地址为:====辽宁省大连市  
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值