Spring Bean生命周期(简单易懂)

Java中的对象不一定是Spring IOC中的Bean,Spring IOC中的Bean一定是Java对象

Bean默认为Singleton(单例),Bean的创建与销毁都归由Spring IOC管理(prototype(原型/多例)除外)

一个对象转为Bean并由Spring IOC管理的过程如下,即一个Bean的生命周期如下

如图所示:

如果简单来讲,笼统概括的话就四步:

1.实例化

2.设置属性值

3.初始化

4.销毁

但是详细来说,会有很多细节需要注意,例如

1.第六步和第七步之间会插入一个@PostConstruct方法

2.一个方式的多种实现,先后顺序的问题,如在bean销毁前的调用发放分为三种实现(注解,实现接口中的方法,xml中bean标签配置),三者的先后顺序为 注解 > 接口方法 > bean标签配置

代码示例:

Bean类:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component(value = "testBean")
public class TestBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    @Override
    public void setBeanName(String s) {
        System.out.println("设置bean的Id........");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("管理我的beanFactory......");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("高级容器接口:applicationContext.......");
    }

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

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean的afterPropertiesSet方法.......");
    }

    @PreDestroy
    private void preDestroyMethod(){
        System.out.println("preDestroyMethod........");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean的destroy方法.........");
    }
BeanPostProcessor接口实现类:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanPostProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("testBean".equalsIgnoreCase(beanName)){
            System.out.println("MyBeanPostProcess--before方法.......");
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if("testBean".equalsIgnoreCase(beanName)){
            System.out.println("MyBeanPostProcess--after方法.......");
        }
        return bean;
    }
}

以上便是自己学习过程中的思想心得,欢迎各路大神前来指导讨论

  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值