Spring Bean的生命周期

1、对象实例化

2、属性设置

3、初始化

4、使用

5、销毁

示例代码如下:

java 复制代码
import org.springframework.stereotype.Component;

@Component
public class SpringBeanA {
    public SpringBeanA() {
        System.out.println("第一步:实例化(spring对象:SpringBeanA)");
    }
}
java 复制代码
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanLifeCycle implements InitializingBean, BeanNameAware, BeanFactoryAware, DisposableBean {

    private SpringBeanA springBeanA;

    @Autowired
    public void setControllerTest(SpringBeanA springBeanA) {
        System.out.println("第二步:属性设置(spring对象注入,@Autowired、@Resource、@Value)");
        this.springBeanA = springBeanA;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("第三步:属性设置后,调用InitializingBean接口的方法");
    }

    @PostConstruct
    public void postConstructMethod() {
        System.out.println("(init方法调用前)@PostConstruct方法调用");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("(init方法调用前)SetBeanFactory回调接口");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("(init方法调用前)setBeanName回调接口, name=" + name);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("第四步:DisposableBean-->destroy方法调用");
    }

    @PreDestroy
    public void testDestroy() {
        System.out.println("第四步(另一种方式):@PreDestroy方法调用");
    }
}
java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfigurationBeanTest {

    @Bean(initMethod = "initMethodTest",destroyMethod = "destroyTest")
    public SpringBean getBean() {
        return new SpringBean();
    }

    public static class SpringBean {
        public void initMethodTest() {
            System.out.println("第三步(另一种方式):@Bean(initMethod属性标识的方法)");
        }
        public void destroyTest() {
            System.out.println("第四步(另一种方式):@Bean(destroyMethod属性标识的方法)");
        }
    }
}

打印如下:

第一步:实例化(spring对象:SpringBeanA)

第二步:属性设置(spring对象注入,@Autowired、@Resource、@Value)

(init方法调用前)setBeanName回调接口, name=springBeanLifeCycle

(init方法调用前)SetBeanFactory回调接口

(init方法调用前)@PostConstruct方法调用

第三步:属性设置后,调用InitializingBean接口的方法

第三步(另一种方式):@Bean(initMethod属性标识的方法)

(Tomcat started)

第四步(另一种方式):@Bean(destroyMethod属性标识的方法)

第四步(另一种方式):@PreDestroy方法调用

第四步:DisposableBean-->destroy方法调用

说明:

实现接口BeanNameAware,可以让SpringBean知道自己的名字,做一些日志记录等操作;实现BeanFactoryAware是为了让SpringBean拥有访问BeanFactory的能力,从而可以根据需要使用beanFactory.getBean()获取别的SpringBean,但是要谨慎使用,因为这个可能会破坏Ioc的原则,尽可能使用@Autowired或者@Value注解使用别的SpringBean

相关推荐
古法老登瞎折腾7 小时前
大数据智能运维实战(十一):运维 MCP 的工程化——权限、限流、审计与错误处理
java·大数据·运维·spring ai·mcp
智能运维指南8 小时前
研制过程“两张皮“怎么破:研制一体化支撑平台的定位、能力边界与落地前提
java·前端·人工智能·嘉为蓝鲸
yz_aiks9 小时前
Spring AI 2.1 ChatMemory 已过时用什么替代:迁移到 Session Memory 实战(JDBC 存储与会话压缩)
java·spring boot·大模型·ai agent·spring ai
金銀銅鐵9 小时前
[Java] 用 GUI 展示 EnumMap 增删键值对时的内部变化
java·后端·ai编程
学长毕设9 小时前
基于SpringBoot的上门洗车服务系统设计与实现
java·spring boot·后端
jason成都9 小时前
RTKLIB-Java 多基线联合RTK:FIX率从0%到91.5%的三层根因修复
java·gnss·rtklib·rtklib_java
鱼宵9 小时前
Stream 终端操作一次讲透:collect、count、reduce 怎么选
java·stream·终端操作
白山编程大哥9 小时前
Java 后端如何优雅地把 List 集合返回给前端
java·前端·list
Linux运维技术栈10 小时前
CVE-2026-91403 高危漏洞修复:Rocky Linux 9 多版本 JDK 环境清理实操
java·linux·运维
On The Road :10 小时前
Java 多线程详解:从入门到线程安全
java·python·安全