Spring Bean 生命周期

Spring Bean 生命周期

一、spring的bean创建和销毁

1、spring只帮我们管理单例bean。

2、spring容器默认是使用单例模式,容器启动时创建所有单例bean,容器关闭时销毁bean

3、对于 prototype 的 bean 多例模式,何时调用getBean对象时创建bean对象,之后则不会再管理后续的生命周期(不会调用destory方法)。

二、注意点

1、BeanPostProcessor 后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行

三、生命周期图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GYOK0gUv-1660437296602)(D:\my_software\Typora\localpicture\image-20220813115036097.png)]

Person 类型

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;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Person implements BeanNameAware,BeanFactoryAware,
        InitializingBean, DisposableBean, ApplicationContextAware {

    private String name;
    private String address;
    private String phone;

    private BeanFactory beanFactory;
    private String beanName;

    public Person() {
        System.out.println("【构造器】调用Person的构造器实例化=======>>>");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("【注入属性】注入属性address");
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        System.out.println("【注入属性】注入属性phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person [address=" + address + ", name=" + name + ", phone="
                + phone + "]";
    }

    // 这是BeanNameAware接口方法
    @Override
    public void setBeanName(String arg0) {
        System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
        this.beanName = arg0;
    }

    // 这是BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out
                .println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
        this.beanFactory = arg0;
    }

    // 这是InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out
                .println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
    }

    // 这是 DisposableBean 接口方法
    @Override
    public void destroy() throws Exception {
        System.out.println("【DisposableBean接口】DisposableBean.destroy()");
    }

    // 通过<bean>的init-method属性指定的初始化方法
    public void myInit() {
        System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过<bean>的destroy-method属性指定的初始化方法
    public void myDestroy() {
        System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("【ApplicationContext 接口】调用ApplicationContext.setApplicationContext()");
    }
}

MyBeanPostProcessor 后置处理器

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

    public MyBeanPostProcessor() {
        super();
        System.out.println("这是 BeanPostProcessor 实现类构造器!!=======>>>");
        // TODO Auto-generated constructor stub
    }

    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out
                .println("【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessAfterInitialization()方法 对属性进行更改!");
        return arg0;
    }

    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out
                .println("【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessBeforeInitialization()方法 对属性进行更改!");
        return arg0;
    }
}	

partBeanLife/beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean id="beanPostProcessor" class="partBeanLife.MyBeanPostProcessor">
    </bean>

    <bean id="person" class="partBeanLife.Person" init-method="myInit" destroy-method="myDestroy"
          p:name="张三" p:address="广州" p:phone="18979696249" />
</beans>

测试类:

public class BeanLifeCycle {
    public static void main(String[] args) {

        System.out.println("现在开始初始化容器");

        ApplicationContext factory = new ClassPathXmlApplicationContext("partBeanLife/beans.xml");
        System.out.println("容器初始化成功");
        //得到Person,并使用
        Person person = factory.getBean("person",Person.class);
        System.out.println(person);

        System.out.println("现在开始关闭容器!");
        ((ClassPathXmlApplicationContext)factory).registerShutdownHook();
    }
}

输出结果:

现在开始初始化容器
这是 BeanPostProcessor 实现类构造器!!=======>>>
【构造器】调用Person的构造器实例化=======>>>
【注入属性】注入属性address
【注入属性】注入属性name
【注入属性】注入属性phone
【BeanNameAware接口】调用BeanNameAware.setBeanName()
【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()
【ApplicationContext 接口】调用ApplicationContext.setApplicationContext()
【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessBeforeInitialization()方法 对属性进行更改!
【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
【init-method】调用<bean>的init-method属性指定的初始化方法
【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessAfterInitialization()方法 对属性进行更改!
容器初始化成功
Person [address=广州, name=张三, phone=18979696249]
现在开始关闭容器!
【DisposableBean接口】DisposableBean.destroy()
【destroy-method】调用<bean>的destroy-method属性指定的初始化方法

[Spring Bean的生命周期(非常详细)]:https://www.cnblogs.com/zrtqsk/p/3735273.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值