资讯专栏INFORMATION COLUMN

spring提供的关于bean生命周期的接口

Cciradih / 2031人阅读

摘要:在中注入注入运行结果注入使用注解正如其名在构造器之后,即在销毁之前。调用的方法构造器注入属性注入顾名思义,在这个方法里面可以拿到所有装载的并在初始化之前对某些进行修改。

先看一张图:spring4.x 企业实战

spring版本:4.3.17
1、bean自身的生命周期接口

1.1、实现 InitializingBean、DisposableBean 接口
这2个接口,会要求你实现2个方法

@Component
public class BeanSelf implements InitializingBean, DisposableBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("destroy");
    }
}

afterPropertieseSet 会在属性设置完毕,调用,这里的属性设置,指的是Bean的依赖注入。比如。在 BeanSelf中注入 BeanSelf2

private BeanSelf2 beanSelf2
@Autowired
public void setBeanSelf2(BeanSelf2 beanSelf2) {
    System.out.println("注入BeanSelf2");
    this.beanSelf2 = beanSelf2
}

运行结果

注入 beanSelf2
afterPropertiesSet
destroy

1.2、使用 @PostConstruct、 @PreDestroy 注解
正如其名:在构造器之后, 即在销毁之前。

public class BeanSelf implements InitializingBean, DisposableBean{

    private BeanSelf2 beanSelf2;
    private BeanSelf3 beanSelf3;

    public BeanSelf(BeanSelf2 beanSelf2) {
        System.out.println("构造器注入 beanSelf2");
        this.beanSelf2 = beanSelf2;
    }

    @Autowired
    public void setBeanSelf3(BeanSelf3 beanSelf3) {
        System.out.println("属性注入 beanSelf3");
        this.beanSelf3 = beanSelf3;
    }

    @PostConstruct
    public void init() {
        System.out.println("PostConstruct");
    }

    @PreDestroy
    public void initDestroy2() {
        System.out.println("PreDestroy");
    }
    
     @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("destroy");
    }
}

运行效果

构造器注入 beanSelf2
属性注入 beanSelf3
PostConstruct   --- 很明显 @PostConstruct 是在构造器之后注入 beanSelf2
afterPropertiesSet --- 在 PostConstruct 之后
PreDestroy   --- 很明显,是在销毁之前调用的
destroy

小总结:不管是@PostConstruct 或者 实现InitializingBean接口。 都是在Bean实例化完成之后才调用的。

2、beanFactory工厂接口,只调用一次

@Component
public class MyBeanFactory implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        Iterator iterator = configurableListableBeanFactory.getBeanNamesIterator();
        BeanSelf2 beanSelf = (BeanSelf2) configurableListableBeanFactory.getBean("beanSelf2");
        beanSelf.add();
        System.out.println("beanFactoryPostProcessor");
    }
}

@Component
public class BeanSelf2 implements InitializingBean{

    public void add() {
        System.out.println("add");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("beanSelf2 afterPropertiesSet");
    }
}

这个接口的方法会在实例化之前调用。 在postProcessBeanFactory 中,对BeanSelf2提前进行初始化,并调用add方法。

beanSelf2 afterPropertiesSet  -- 调用beanSelf2的afterPropertiesSet方法 
add 
beanFactoryPostProcessor
构造器注入 beanSelf2
属性注入 beanSelf3
PostConstruct
afterPropertiesSet
PreDestroy

BeanFactoryPostProcessor 顾名思义,在这个方法里面可以拿到所有装载的Bean,并在初始化之前对某些Bean进行修改。(此时Bean还未初始化)

3、BeanPostProcessor接口在每个Bean实例之前,都会调用。如果Bean已实例化则不会diaoy

@Component
public class MyBeanPostProcessor implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName +"  =  postProcessBeforeInitialization" );
        if("beanSelf2".equals(beanName)) {
            return null;
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName +"  =  postProcessAfterInitialization");
        return bean;
    }
}

上面这段代码的意思是,在初始化之前,将BeanSelf2 和 BeanSel3 置为null。但是,BeanSelf2不会走到这段代码内,因为在接口BeanFactoryPostProcessor 中,将BeanSelf2提前初始化了。所以输出结果如下

beanSelf2 afterPropertiesSet  --- BeanFactoryPostProcessor中提前初始化
add                        ---  调用BeanSelf2的add方法
beanFactoryPostProcessor   --- 在postProcessBeanFactory 中打印
beanConfig  =  postProcessBeforeInitialization  --调用 BeanPostProcessor
beanConfig  =  postProcessAfterInitialization   --调用 BeanPostProcessor
BeanSelf  构造器注入 beanSelf2                   --BeanSelf 构造器注入属性
beanSelf3  =  postProcessBeforeInitialization  -- 在实例化之前调用 
BeanSelf  属性注入 beanSelf3                    -- 注入之前,BeanSelf3还没实例化,所以在之前调用 BeanPostProcessor
beanSelf  =  postProcessBeforeInitialization   --- beanSelf 在属性设置完毕后,即初始化完毕 调用 BeanPostProcessor#postProcessBeforeInitialization()
BeanSelf  PostConstruct  --  调用被 @PostConstruct 注解声明的方法
afterPropertiesSet     -- 调用 InitializingBean 接口实现的方法
beanSelf  =  postProcessAfterInitialization
beanSelf2 com.xhn3.BeanSelf2@33cb5951
beanSelf3 null      -- 在BeanPostProcessor中返回null。所以这边是null
BeanSelf  PreDestroy
destroy

小总结:BeanPostProcessor#postProcessBeforeInitialization在init-method之前调用,在属性设置完毕后调用。BeanPostProcessor#postProcessAfterInitialization 在InitializingBean#afterPropertiesSet后调用。

4、BeanDefinitionRegistryPostProcessor 注册Bean的接口,在BeanFactoryPostProcesso前调用

@Component
public class MyBeanRegister implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
   
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }
}

在该方法里面可以直接注册bean。可以提前实例化Bean

运行流程:

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69655.html

相关文章

  • Spring Aware 到底是什么?

    摘要:代码示例自定义实现注册运行和预想一样,输出结果为,如果移除掉注解的属性,输出结果为总结在大多数情况下,我们应该避免使用任何接口,除非我们需要它们。 showImg(https://segmentfault.com/img/remote/1460000019807821?w=1920&h=1080); 通过如下前序两篇文章: Spring Bean 生命周期之我从哪里来? Spring...

    mingzhong 评论0 收藏0
  • Spring详解2.理解IoC容器

    摘要:目前建议使用与。入參是当前正在处理的,是当前的配置名,返回的对象为处理后的。如果,则将放入容器的缓存池中,并返回。和这两个接口,一般称它们的实现类为后处理器。体系结构让容器拥有了发布应用上下文事件的功能,包括容器启动事件关闭事件等。 点击进入我的博客 1 如何理解IoC 1.1 依然是KFC的案例 interface Burger { int getPrice(); } in...

    Ververica 评论0 收藏0
  • Spring IOC知识点一网打尽!

    摘要:使用的好处知乎的回答不用自己组装,拿来就用。统一配置,便于修改。 前言 只有光头才能变强 回顾前面: 给女朋友讲解什么是代理模式 包装模式就是这么简单啦 单例模式你会几种写法? 工厂模式理解了没有? 在刷Spring书籍的时候花了点时间去学习了单例模式和工厂模式,总的来说还是非常值得的! 本来想的是刷完《Spring 实战 (第4版)》和《精通Spring4.x 企业应用开发实战》...

    djfml 评论0 收藏0
  • Spring之旅第二站:bean、新特性。。。

    摘要:除了,还简单介绍了对的支持,可以帮助应用将散落在各处的逻辑汇集于一处切面。当装配的时候,这些切面能够运行期编织起来,这样就能呢个非常有效的赋予新功能。 第1章 Spring之旅 说明 1、本文参考了《Spring 实战》重点内容,参考了GitHub上的代码 2、每个人的学习方式不一样,但目的是一样的,活学活用。最近一直在听《我们不一样》 3、本文只为记录作为以后参考,要想真正领悟Sp...

    luodongseu 评论0 收藏0
  • Spring Bean 生命周期之destroy——终极信仰

    摘要:上一篇文章生命周期之我从哪里来说明了我是谁和我从哪里来的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去初始化有三种方式销毁同样有三种方式正所谓,天对地,雨对风对对对雷隐隐,雾蒙蒙山花对海树,赤日对苍穹平仄平仄平平仄,仄平仄平仄 上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去?sho...

    JouyPub 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<