写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor

本文详细介绍了Spring框架中Bean的初始化过程,包括init-method方法、afterPropertiesSet方法和BeanPostProcessor接口的应用。通过具体代码示例展示了这些方法的执行顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1、init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置。init-method需要在applicationContext.xml配置文档中bean的定义里头写明。例如:<bean id="TestBean" class="nju.software.xkxt.util.TestBean" init-method="init"></bean>

这样,当TestBean在初始化的时候会执行TestBean中定义的init方法。

 

2、afterPropertiesSet方法,初始化bean的时候执行,可以针对某个具体的bean进行配置。afterPropertiesSet 必须实现 InitializingBean接口。实现 InitializingBean接口必须实现afterPropertiesSet方法。

 

3、BeanPostProcessor,针对所有Spring上下文中所有的bean,可以在配置文档applicationContext.xml中配置一个BeanPostProcessor,然后对所有的bean进行一个初始化之前和之后的代理。BeanPostProcessor接口中有两个方法: postProcessBeforeInitialization和postProcessAfterInitialization。 postProcessBeforeInitialization方法在bean初始化之前执行, postProcessAfterInitialization方法在bean初始化之后执行。

 

总之,afterPropertiesSet 和init-method之间的执行顺序是afterPropertiesSet 先执行,init-method 后执行。从BeanPostProcessor的作用,可以看出最先执行的是postProcessBeforeInitialization,然后是afterPropertiesSet,然后是init-method,然后是postProcessAfterInitialization。

 

二、相关用法及代码测试

1、PostProcessor类,实现BeanPostProcessor接口,实现接口中的postProcessBeforeInitialization,postProcessAfterInitialization方法

[java] view plain copy

  1. package nju.software.xkxt.util;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanPostProcessor;  
  5.   
  6. /** 
  7.  * 定义Bean初始化前后的动作 
  8.  *  
  9.  * @author typ 
  10.  *  
  11.  */  
  12. public class PostProcessor implements BeanPostProcessor {  
  13.     @Override  
  14.     public Object postProcessBeforeInitialization(Object bean, String beanName)  
  15.             throws BeansException {  
  16.         System.out.println("------------------------------");  
  17.         System.out.println("对象" + beanName + "开始实例化");  
  18.         return bean;  
  19.     }  
  20.   
  21.     @Override  
  22.     public Object postProcessAfterInitialization(Object bean, String beanName)  
  23.             throws BeansException {  
  24.         System.out.println("对象" + beanName + "实例化完成");  
  25.         System.out.println("------------------------------");  
  26.         return bean;  
  27.     }  
  28.   
  29. }  

 

该PostProcessor类要作为bean定义到applicationContext.xml中,如下

<bean class="nju.software.xkxt.util.PostProcessor"></bean>

 

2、TestBean类,用做测试Bean,观察该Bean初始化过程中上面4个方法执行的先后顺序和内容。实现InitializingBean接口,并且实现接口中的afterPropertiesSet方法。最后定义作为init-method的init方法。

[java] view plain copy

  1. package nju.software.xkxt.util;  
  2.   
  3. import org.springframework.beans.factory.InitializingBean;  
  4.   
  5. /** 
  6.  * 用做测试Bean,观察该Bean初始化过程中上面4个方法执行的先后顺序和内容 
  7.  *  
  8.  * @author typ 
  9.  *  
  10.  */  
  11. public class TestBean implements InitializingBean {  
  12.     String name;  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.   
  22.     public void init() {  
  23.         System.out.println("init-method is called");  
  24.         System.out.println("******************************");  
  25.     }  
  26.   
  27.     @Override  
  28.     public void afterPropertiesSet() throws Exception {  
  29.         System.out.println("******************************");  
  30.         System.out.println("afterPropertiesSet is called");  
  31.         System.out.println("******************************");  
  32.     }  
  33. }  

 

启动Tomcat服务器,可以看到服务器启动过程中,完成对Bean进行初始化。执行结果如下:

 

------------------------------

对象TestBean开始实例化

******************************

afterPropertiesSet is called

******************************

init-method is called

******************************

对象TestBean实例化完成

------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值