Spring Bean的生命周期

本文详细解析了Spring IoC容器中Bean的生命周期,包括初始化、依赖注入及销毁过程。通过具体代码示例,展示了如何利用BeanPostProcessor、InitializingBean、DisposableBean等接口及init-method和destroy-method属性自定义Bean的生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring IoC容器的本质目的就是为了管理Bean,对于Bean而言,我们就要了解它的生命周期。
在这里插入图片描述
从上图可以看到,Spring IoC容器对Bean的管理还是比较复杂的,Spring IoC容器在执行了初始化和依赖注入后,会执行一定的步骤来完成初始化,通过这些步骤我们就可以自定义初始化,而在Spring IoC容器正常关闭的时候,他也会执行一定的步骤来关闭容器,释放资源。

下面我用一个实现类来测试一下他的生命周期。

测试文件框架
在这里插入图片描述
BeanPostProcessorImpl.java

package Juice.main.bean;

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

public class BeanPostProcessorImpl implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException{
        System.out.println("【"+bean.getClass().getSimpleName()+"】对象"+beanName+"开始实例化");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException{
        System.out.println("【"+bean.getClass().getSimpleName()+"】对象"+beanName+"实例化完成");
        return bean;
    }

}

JuiceMaker.java

package Juice.main.pojo;

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

public class JuiceMaker
        implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    private String beverageShop = null;
    private Source source = null;

    public String getBeverageShop() {
        return beverageShop;
    }

    public void setBeverageShop(String beverageShop) {
        this.beverageShop = beverageShop;
    }

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    public void init(){
        System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义初始化方法");
    }

    public void myDestory(){
        System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义销毁方法");
    }

    public String makeJuice(){
        String Juice="这是一杯由"+beverageShop+"饮品店,提供的"+source.getSize()+source.getSugar()+source.getFruit();
        return Juice;
    }

    @Override
    public void setBeanName(String arg0){
        System.out.println("【"+this.getClass().getSimpleName()+"】调用BeanNameAware接口的setBeanName方法");
    }

    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException{
        System.out.println("【"+this.getClass().getSimpleName()+"】调用BeanFactoryAware接口的setBeanFactory方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException{
        System.out.println("【"+this.getClass().getSimpleName()+"】调用ApplicationContextAware接口的setApplicationContext方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception{
        System.out.println("【"+this.getClass().getSimpleName()+"】调用InitializingBean接口的afterPropertiesSet方法");
    }

    @Override
    public void destroy() throws Exception{
        System.out.println("调用接口DisposableBean的destory方法");
    }
}

Source.java

package Juice.main.pojo;

public class Source {
    private String fruit;   //类型
    private String sugar;   //糖分描述
    private Integer size;   //大小杯

    public String getFruit() {
        return fruit;
    }

    public void setFruit(String fruit) {
        this.fruit = fruit;
    }

    public String getSugar() {
        return sugar;
    }

    public void setSugar(String sugar) {
        this.sugar = sugar;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }
}

spring-cfg.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <bean id="beanPostProcessor"
          class="Juice.main.bean.BeanPostProcessorImpl"></bean>

    <bean id="source" class="Juice.main.pojo.Source">
        <property name="fruit" value="橙汁"></property>
        <property name="size" value="2"></property>
        <property name="sugar" value="少糖"></property>
    </bean>

    <bean id="juiceMaker" class="Juice.main.pojo.JuiceMaker"
        init-method="init" destroy-method="myDestory">
        <property name="beverageShop" value="贡茶"></property>
        <property name="source" ref="source"></property>
    </bean>

</beans>

test.java

package Juice.test;

import Juice.main.pojo.JuiceMaker;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args){
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("spring-cfg.xml");
        JuiceMaker juiceMaker=(JuiceMaker)ctx.getBean("juiceMaker");
        System.out.println(juiceMaker.makeJuice());
        ctx.close();
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值