Spring之Bean的生命周期过程中调用的方法

1。这个部分除了6,9都在这了

java 复制代码
package com.example.springbootdemo3.lifebeean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * description
 *
 * @author PC 2025/02/27 20:26
 */
@Component
public class MyBeanLife implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {


    private String name;

    // 构造函数,当Bean被实例化时调用
    public MyBeanLife() {
        System.out.println("1.调用了Bean的构造函数");
    }

    @Value("name")
    public void setName(String name) {
        this.name = name;
        System.out.println("2.调用了Bean的set方法(属性的依赖注入)");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("3.调用了BeanNameAware的setBeanName方法,beanName:" + s);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("4.调用了BeanFactoryAware的setBeanFactory方法,beanFactory:" + beanFactory.toString());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("5.调用了ApplicationContextAware的setApplicationContext方法,applicationContext:" + applicationContext.toString());
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("7.调用了@PostConstruct注解的方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("8.调用了InitializingBean的afterPropertiesSet方法");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("10.调用了@PreDestroy注解的方法");
    }

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

2. 6和9部分

BeanPostProcessor的两个方法是所有Bean都会调用,除了实现这个接口的Bean,所以为了方便加了判断

java 复制代码
package com.example.springbootdemo3.lifebeean;

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

/**
 * description
 *
 * @author PC 2025/02/27 21:00
 */
@Component
public class MyBeanLife2 implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (!beanName.equals("myBeanLife")) {
            return bean;
        }
        System.out.println("6.调用了BeanPostProcessor的postProcessBeforeInitialization方法,beanName:" + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (!beanName.equals("myBeanLife")) {
            return bean;
        }
        System.out.println("9.调用了BeanPostProcessor的postProcessAfterInitialization方法,beanName:" + beanName);
        return bean;
    }
}
相关推荐
逐光者933几秒前
滤波器的 R 与 C 如何选择:从截止频率到工程实践
c语言·开发语言·r语言
Eider19986 分钟前
大模型输出格式幻觉解决思路
开发语言·前端·javascript
Wang's Blog6 分钟前
Java 项目实战: 交友项目 - 入门案例:编写 Dubbo 服务消费者
java·dubbo·交友
IT研究室14 分钟前
最新计算机毕业设计选题推荐-基于spring boot的教育学习平台-网站-文档指导-Java-springboot
java·spring boot·课程设计
亚林瓜子16 分钟前
Spring Boot 集成 ip2region 离线 IP 库实战指南
java·spring boot·后端·ip·ip2region
Want59524 分钟前
Python太极八卦图
开发语言·python
饕餮争锋35 分钟前
python开发工具uv简介
开发语言·python·uv
盛世宏博智慧档案43 分钟前
恒湿净化一体机联动开发:档案馆温湿度阈值判断与启停控制实践
java·后端·struts·恒湿机·加湿除湿消毒净化一体机
奇树谦1 小时前
LRU 缓存原理与 C++ 实践示例
开发语言·c++·缓存
toooooop81 小时前
FastAdmin Hook 机制与插件错误透传:`$result` 之外的信封
开发语言·fastadmin