[关闭]
@ghimi 2018-10-14T13:27:25.000000Z 字数 2773 阅读 968

Bean 初始化之 postProcessorInitialization,afterPropertiesSet,init-method,postProcessAfterInitialization 等方法的加载

未分类


一, 简单介绍

  1. init-method 方法,初始化 bean 的时候执行,可以针对某个具体的 bean 进行配置.init-method 需要在 applicationContext.xml 配置文档的 bean 定义里头写明.例如

    <bean id="TestBean" class="nju.software.xkxt.util.TestBean" init-method="init"></bean>
  2. afterPropertiesSet 方法,初始化 bean 的时候执行,可以针对某个具体的 bean 进行配置.afterPropertiesSet 必须实现 InitializingBean 接口.实现 InitialzingBean 接口必须实现 afterPropertiesSet 方法.
  3. BeanPostProcessor,针对所有的 Spring 上下文中所有的 bean ,可以在配置文档 applicationContext.xml 中配置一个 BeanPostProcessor ,然后对所有的 bean 进行一个 初始化之前和之后的代理.BeanPostProcessor 接口中有两个方法: postProcessBeforeInitialization 和 postProcessAfterInitailization .postProcessBeforeInitialization 方法在 bean 初始化之前执行,postProcessAfterInitialization 方法在 bean 初始化之后执行.

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

相关用法及代码测试

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

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

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

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

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

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

  1. ------------------------------
  2. 对象TestBean开始实例化
  3. ******************************
  4. afterPropertiesSet is called
  5. ******************************
  6. init-method is called
  7. ******************************
  8. 对象TestBean实例化完成
  9. ------------------------------
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注