BeanPostProcessors使用总结

来源:互联网 发布:基于java博客 编辑:程序博客网 时间:2024/04/30 09:16

1、bean生成过程



2、BeanPostProcessors接口


如果这个接口的某个实现类被注册到某个容器,那么该容器的每个受管Bean在调用初始化方法之前,都会获得该接口实现类的一个回调。容器调用接口定义的方法时会将该受管Bean的实例和名字通过参数传入方法,进过处理后通过方法的返回值返回给容器。


要使用BeanPostProcessor回调,就必须先在容器中注册实现该接口的类,那么如何注册呢?BeanFactory和ApplicationContext容器的注册方式不大一样:若使用BeanFactory,则必须要显示的调用其addBeanPostProcessor()方法进行注册,参数为BeanPostProcessor实现类的实例;如果是使用ApplicationContext,那么容器会在配置文件在中自动寻找实现了BeanPostProcessor接口的Bean,然后自动注册,我们要做的只是配置一个BeanPostProcessor实现类的Bean就可以了

假如我们使用了多个的BeanPostProcessor的实现类,那么如何确定处理顺序呢?其实只要实现Ordered接口,设置order属性就可以很轻松的确定不同实现类的处理顺序了。



3、示例


3.1 applicationContext.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  9.        http://www.springframework.org/schema/aop  
  10.        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.        http://www.springframework.org/schema/context  
  12.        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  13.   
  14.   
  15.     <!-- 声明注解方式加载bean-->  
  16.     <context:annotation-config/>  
  17.   
  18.     <!-- 要加载的bean的包路径-->  
  19.     <context:component-scan base-package="com.meituan.hyt.test1"/>  
  20.   
  21.   
  22.     <bean id="userPostProcessor" class="com.meituan.hyt.test1.UserPostProcessor"/>  
  23. </beans>  

3.2 自己的业务bean

[java] view plaincopyprint?
  1. package com.meituan.hyt.test1;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. public class User {  
  8.     @Value("老名字")  
  9.     private String name;  
  10.     @Value("50")  
  11.     private Integer id;  
  12.   
  13.     public Integer getId() {  
  14.         return id;  
  15.     }  
  16.   
  17.     public void setId(Integer id) {  
  18.         this.id = id;  
  19.     }  
  20.   
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     @Override  
  30.     public String toString() {  
  31.         return "User{" +  
  32.                 "name='" + name + '\'' +  
  33.                 ", id=" + id +  
  34.                 '}';  
  35.     }  
  36. }  

3.3 postProcessor bean

[java] view plaincopyprint?
  1. package com.meituan.hyt.test1;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanPostProcessor;  
  5.   
  6.   
  7. public class UserPostProcessor implements BeanPostProcessor {  
  8.   
  9.     @Override  
  10.     public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {  
  11.         if(o instanceof User){  
  12.             User user = (User)o;  
  13.             user.setName("新名字");  
  14.             user.setId(100);  
  15.             return user;  
  16.         }  
  17.         return o;  
  18.   
  19.     }  
  20.   
  21.     @Override  
  22.     public Object postProcessAfterInitialization(Object o, String s) throws BeansException {  
  23.         return o;  
  24.     }  
  25. }  

3.4 测试方法

[java] view plaincopyprint?
  1. package com.meituan.hyt.test1;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6.   
  7. public class Main2 {  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");  
  10.         User user = (User) cxt.getBean("user");  
  11.         System.out.println(user.toString());  
  12.     }  
  13. }  

3.5 执行结果


如果没有<bean id="userPostProcessor" class="com.meituan.hyt.test1.UserPostProcessor"/>
User{name='老名字', id=50}

添加<bean id="userPostProcessor" class="com.meituan.hyt.test1.UserPostProcessor"/>
User{name='新名字', id=100}

0 0
原创粉丝点击