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

相关推荐
鲸采云SRM采购管理系统20 分钟前
采购管理系统哪家好?2026最新测评对比
java·服务器·数据库
0x531 小时前
Java网络编程(二)
java·服务器·网络
Flittly2 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
Poo_Chai2 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
瑞码空间2 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
树码小子2 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
茶本无香2 小时前
Java调用Shell脚本执行SQL数据库操作:从入门到实战
java·sql·shell
风流 少年2 小时前
Spring AI 2.0:MCP
java·后端·spring
天疆说3 小时前
01 硬件选型与 llama.cpp 部署:4× RTX 5880 Ada 跑 DeepSeek-V4-Flash
java·redis·llama