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

相关推荐
腻害兔4 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
码智社5 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
萧瑟余晖6 小时前
JDK 26 新特性详解
java·开发语言
马优晨7 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
维天说9 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
Zane199410 小时前
并发 vs 并行:别再傻傻分不清了,一文讲透 Java 并发编程的第一课
java·后端
噢,我明白了11 小时前
java中Excel的导入和导出(EasyExcel)
java·开发语言·excel
吠品11 小时前
Zabbix Web界面误报Server未运行的排查与解决
java·服务器·数据库
啊湘11 小时前
天气查询API接口 按月Token鉴权 实时天气 物联网可用 文档齐全
java·后端·struts
Java内核笔记11 小时前
告别十亿美元的错误 : Spring Boot 4 空安全 (JSpecify) 实战
java·spring boot·后端