简单比较init-method,afterPropertiesSet和BeanPostProcessor
来源:互联网 发布:视频收费网站源码 编辑:程序博客网 时间:2024/06/04 18:38
BeanPostProcessor
BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化、配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现,然后注册到容器中。
Spring中Bean(非实现beanpostprocessor的类)的实例化过程图示:
由上图可以看到,Spring中的BeanPostProcessor在实例化过程处于的位置,BeanPostProcessor接口有两个方法需要实现:postProcessBeforeInitialization和postProcessAfterInitialization,
一、简单介绍
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方法
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * 定义Bean初始化前后的动作 * * */ public class PostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("------------------------------"); System.out.println("对象" + beanName + "开始实例化"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("对象" + beanName + "实例化完成"); System.out.println("------------------------------"); return bean; } }
该PostProcessor类要作为bean定义到applicationContext.xml中,如下
自己的名字
<bean class="XXX.util.PostProcessor"></bean>
2、TestBean类,用做测试Bean,观察该Bean初始化过程中上面4个方法执行的先后顺序和内容。实现InitializingBean接口,并且实现接口中的afterPropertiesSet方法。最后定义作为init-method的init方法。
import org.springframework.beans.factory.InitializingBean; /** * 用做测试Bean,观察该Bean初始化过程中上面4个方法执行的先后顺序和内容 * * */ public class TestBean implements InitializingBean { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void init() { System.out.println("init-method is called"); System.out.println("******************************"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("******************************"); System.out.println("afterPropertiesSet is called"); System.out.println("******************************"); } }
启动Tomcat服务器,可以看到服务器启动过程中,完成对Bean进行初始化。执行结果如下:
------------------------------
对象TestBean开始实例化
******************************
afterPropertiesSet is called
******************************
init-method is called
******************************
对象TestBean实例化完成
------------------------------
阅读全文
0 0
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor
- init-method 与afterPropertiesSet
- 源码解析:init-method、@PostConstruct、afterPropertiesSet孰先孰后
- Spring的init-method 与afterPropertiesSet
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- Spring InitializingBean的afterPropertiesSet与init-method、@PostConstruct
- 从源码实现的角度解析spring的InitializingBean的afterPropertiesSet方法和init-method区别和应用
- InitializingBean和init-method
- 在spring的init-method指定的方法之前做一些事情要用BeanPostProcessor
- Spring (init-method和destroy-method )
- Spring 的 init-method 和 destory-method
- Spring的init-method和destory-method
- LTE之物理层上下行
- Servlet开发步骤
- C#之入门总结_字符串_09
- 二叉堆模板(备用)
- 延迟关联(解决海量数据分页优化)
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 当前流行5大css前端框架(2017.5)
- Object Detection
- HDU4027 Can you answer these queries?(线段树区间更新,区间开根号)
- Java封装性、构造方法、匿名对象
- docker初探---第一季
- Json--后台将List集合转换成json数据
- html中常见的小经验
- HDU6059-Kanade's trio