大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > Spring Bean的生命周期(非常详细)

Spring Bean的生命周期(非常详细)(2)

关键词:生命周期SpringBean  阅读(1334) 赞(16)

[摘要]本文是对Spring Bean的生命周期(非常详细)的讲解,对学习Java编程技术有所帮助,与大家分享。
 package springBeanTest;
 
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 
 public class MyBeanPostProcessor implements BeanPostProcessor {
 
     public MyBeanPostProcessor() {
         super();
         System.out.println("这是BeanPostProcessor实现类构造器!!");
         // TODO Auto-generated constructor stub
     }
 
     @Override
     public Object postProcessAfterInitialization(Object arg0, String arg1)
             throws BeansException {
         System.out
         .println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!");
         return arg0;
     }
 
     @Override
     public Object postProcessBeforeInitialization(Object arg0, String arg1)
             throws BeansException {
         System.out
         .println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!");
         return arg0;
     }
 }

如上,BeanPostProcessor接口包括2个方法postProcessAfterInitialization和postProcessBeforeInitialization,这两个方法的第一个参数都是要处理的Bean对象,第二个参数都是Bean的name。返回值也都是要处理的Bean对象。这里要注意。

3、InstantiationAwareBeanPostProcessor 接口本质是BeanPostProcessor的子接口,一般我们继承Spring为其提供的适配器类InstantiationAwareBeanPostProcessor Adapter来使用它,如下:

 package springBeanTest;
 
 import java.beans.PropertyDescriptor;
 
 import org.springframework.beans.BeansException;
 import org.springframework.beans.PropertyValues;
 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
 
 public class MyInstantiationAwareBeanPostProcessor extends
         InstantiationAwareBeanPostProcessorAdapter {
 
     public MyInstantiationAwareBeanPostProcessor() {
         super();
         System.out
                 .println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!");
     }
 
     // 接口方法、实例化Bean之前调用
     @Override
     public Object postProcessBeforeInstantiation(Class beanClass,
             String beanName) throws BeansException {
         System.out
                 .println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
         return null;
     }
 
     // 接口方法、实例化Bean之后调用
     @Override
     public Object postProcessAfterInitialization(Object bean, String beanName)
             throws BeansException {
         System.out
                 .println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
         return bean;
     }
 
     // 接口方法、设置某个属性时调用
     @Override
     public PropertyValues postProcessPropertyValues(PropertyValues pvs,
             PropertyDescriptor[] pds, Object bean, String beanName)
             throws BeansException {
         System.out
                 .println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
         return pvs;
     }
 }

这个有3个方法,其中第二个方法postProcessAfterInitialization就是重写了BeanPostProcessor的方法。第三个方法postProcessPropertyValues用来操作属性,返回值也应该是PropertyValues对象。



相关评论