博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Bean在BeanFactory生命周期
阅读量:2492 次
发布时间:2019-05-11

本文共 5807 字,大约阅读时间需要 19 分钟。

生命周期过程

  • 如果容器注册InstantiationAwareBeanPostProcessor接口,调用postProcessBeforeInstantiation方法
  • Bean的实例化(调用默认构造器)
  • 如果容器注册InstantiationAwareBeanPostProcessor接口,调用postProcessAfterInstantiation方法
  • 如果容器注册InstantiationAwareBeanPostProcessor接口,调用postProcessPropertyValues方法
  • 根据配置设置属性值
  • 如果Bean实现了BeanNameAware接口,调用BeanNameAware接口的setBeanName方法
  • 如果Bean实现了BeanFactoryAware 接口,调用BeanFactoryAware 接口的setBeanFactory方法
  • 如果容器注册了BeanPostProcessor接口,调用BeanPostProcessor接口的postProcessBeforeInitialization方法
  • 如果Bean实现了InitializingBean接口,调用InitializingBean接口的afterPropertiesSet方法
  • 通过init-method属性配置的初始方法
  • 如果容器注册了BeanPostProcessor接口,调用BeanPostProcessor接口的postProcessAfterInitialization 方法
  • 如果是单例模式,将Bean放入缓存池中;容器销毁时,调用DisposableBean的destroy方法;最后调用destroy-method方法
  • 如果是多例模式,将Bean交给调用者。

测试

package exa.ydoing.spring;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.config.ConfigurableBeanFactory;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class App {    public static void main(String[] args) {        //实例化BeanFactory        Resource res = new ClassPathResource("ApplicationContext.xml");        BeanFactory bf = new XmlBeanFactory(res);        //注册InstantiationAwareBeanPostProcessor后置处理器,因为InstantiationAwareBeanPostProcessor继承了BeanPostProcessor,        //所以不再单独注册BeanPostProcessor后置处理器        ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());        //获得bean        MyBean bean = bf.getBean("myBean", MyBean.class);        bean.printName();        //销毁所有Bean        ((XmlBeanFactory)bf).destroySingletons();    }}

InstantiationAwareBeanPostProcessor 的实现:

package exa.ydoing.spring;import java.beans.PropertyDescriptor;import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用BeanPostProcessor接口的postProcessBeforeInitialization方法"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用BeanPostProcessor接口的postProcessAfterInitialization方法"); return bean; } @Override public Object postProcessBeforeInstantiation(Class
beanClass, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法"); return null; } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法"); return true; } @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法"); return pvs; }}

MyBean的代码:

package exa.ydoing.spring;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class MyBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean{
private BeanFactory beanFactory; private String name; private String tag; public MyBean(){ System.out.println("调用构造器实例化Bean"); } public void printName(){ System.out.println("MyBean name is: " + name); } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; System.out.println("设置Bean的属性为:" + tag); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("调用DisposableBean接口的destroy方法"); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("调用InitializingBean接口的afterPropertiesSet方法"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { // TODO Auto-generated method stub this.beanFactory = beanFactory; System.out.println("调用BeanFactoryAware接口的setBeanFactory方法"); } @Override public void setBeanName(String name) { // TODO Auto-generated method stub this.name = name; System.out.println("调用BeanNameAware接口的setBeanName方法"); }}

Bean的配置很简单:


输出:

调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法调用构造器实例化Bean调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法设置Bean的属性为:test调用BeanNameAware接口的setBeanName方法调用BeanFactoryAware接口的setBeanFactory方法调用BeanPostProcessor接口的postProcessBeforeInitialization方法调用InitializingBean接口的afterPropertiesSet方法调用BeanPostProcessor接口的postProcessAfterInitialization方法MyBean name is: myBean调用DisposableBean接口的destroy方法

转载地址:http://fxhrb.baihongyu.com/

你可能感兴趣的文章
C语言理论作业—2
查看>>
(Problem 6)Sum square difference
查看>>
SSIS 学习之旅 FTP访问类
查看>>
进程动态优先级调度
查看>>
MyBatis入门教程(基于Mybatis3.2)
查看>>
POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
查看>>
matplotlib 第二次执行报错在 django web服务中
查看>>
多项目开发下的dll文件管理
查看>>
bzoj4773 负环
查看>>
记录一下Junit测试MongoDB,获取MongoTemplate
查看>>
Kali Linux 下渗透测试 | 3389 批量爆破神器 | hydra | 内网渗透测试
查看>>
json-server模拟后台接口
查看>>
C#笔记
查看>>
Java线程池
查看>>
winform中键盘和鼠标事件的捕捉和重写(转)
查看>>
freemarker跳出循环
查看>>
关于移动端的一些tip
查看>>
进销存和财务方面业务知识了解
查看>>
ios 内存管理 心得
查看>>
OSI
查看>>