关于springboot启动过程中BeanPostProcessor及其他初始化方法的问题

在网上学习bean的生命周期时遇到了一些问题

1.网上看到的

1.有说afterPropertiesSet方法和自定义init方法是夹杂在bean的前后置处理器是在之间执行的。

2.也有说自定义init方法和@PostConstruct是夹杂在bean的前后置处理器是在之间执行:

找了好多地方说法各不相同,但都是表明自定义init方法是夹杂在bean的前后置处理器是在之间执行的

2、实操

先放结果

可以看到

1.afterPropertiesSet方法是最先执行的

2.前置处理器和后置处理器是一起执行的,中间没有其他的方法

3.自定义init方法最后执行

代码

实际bean

java 复制代码
package com.example.myspringboot.cunstom;  
  
import org.springframework.beans.factory.Aware;  
import org.springframework.beans.factory.BeanNameAware;  
import org.springframework.stereotype.Service;  
  
@Service  
public class CustomService implements BeanNameAware {  
  
    public CustomService(){  
        System.out.println("===============调用了CustomService的构造方法=====================");  
    }  
  
    public void init(){  
        System.out.println("===============在bean内部实现,通过@Bean(initMethod=\"init\")指定的初始化方法======================");  
    }  
  
  
    public String achieve(String s1){  
        System.out.println("===============bean内实际业务代码,接收参数:"+s1+"======================");  
        return s1;  
    }  
  
    @Override  
    public void setBeanName(String s) {  
        System.out.println("===============aware接口 setBeanName======================");  
    }  
}

afterPropertiesSet

java 复制代码
package com.example.myspringboot.config;  
  
import org.springframework.beans.factory.InitializingBean;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.ApplicationContextAware;  
import org.springframework.stereotype.Service;  
  
@Service  
public class CustomInitializeBean implements InitializingBean , ApplicationContextAware {  
  
  
    ApplicationContext applicationContext;  
    @Override  
    public void afterPropertiesSet() throws Exception {  
    //通过ApplicationContextAware applicationContext获取bean  
    // CustomBean bean = applicationContext.getBean(CustomBean.class);  
    //上述代码报错,此时还获取不到bean ? 
    System.out.println("======================================afterPropertiesSet===============================================");  
  
    }  
  
  
    @Override  
    public void setApplicationContext(ApplicationContext applicationContext) {  
        this.applicationContext = applicationContext;  
    }  
}

BeanPostProcessors

java 复制代码
package com.example.myspringboot.config;  
  
import org.springframework.beans.BeansException;  
import org.springframework.beans.factory.config.BeanPostProcessor;  
import org.springframework.core.PriorityOrdered;  
import org.springframework.stereotype.Service;  
  
@Service  
public class CustomApplyBeanPostProcessors implements BeanPostProcessor, PriorityOrdered {  
    @Override  
    public int getOrder() {  
        return 0;  
    }  
  
    @Override  
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {  
  
        if ("customService".equals(beanName)){  
            System.out.println("==========序号0的前置处理器:"+beanName+";;"+bean.toString());  
        }  
  
        // return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);  
        return bean;  
    }  
  
    @Override  
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {  
        if ("customService".equals(beanName)){  
            System.out.println("==========序号0的后置处理器:"+beanName+";;"+bean.toString());  
        }  
        // return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);  
        return bean;  
    }  
}

Configuration

java 复制代码
package com.example.myspringboot.config;  
  
import com.example.myspringboot.cunstom.CustomService;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
  
@Configuration  
public class CustomBeanConfiguration {  
  
    @Bean(initMethod = "init")  
    public CustomService customBeanService2(){  
        return new CustomService();  
    }  
}

其他注解

java 复制代码
package com.example.myspringboot.config;  
  
import org.springframework.stereotype.Service;  
  
import javax.annotation.PostConstruct;  
import javax.annotation.PreDestroy;  
  
@Service  
public class CustomOther {  
  
    @PostConstruct  
    public void init(){  
        System.out.println("================@PostConstruct 注解的处理方法=========");  
    }  
  
    @PreDestroy  
    public void destory(){  
        System.out.println("================@PreDestroy 注解的处理方法=========");  
    }  
}

问题

1.为什么我这个执行顺序不太对?为什么自定义的init方法是在最后执行?

个人感觉afterPropertiesSet应该就是在最前面执行的。毕竟直译就是是"参数设置之后",前后置处理器是跟随每一个bean都要执行一遍,如果afterPropertiesSet夹在前后置处理器之间岂不是也要跟随每个bean都执行一遍?

2.执行了两遍构造方法,第二遍是在创建代理对象吗?如果是,为什么自定义的init方法会只跟随代理对象执行

把自己学迷糊了,求大佬们帮忙解答一下吧。T_T

相关推荐
kali-Myon2 小时前
2025春秋杯网络安全联赛冬季赛-day1
java·sql·安全·web安全·ai·php·web
我是咸鱼不闲呀3 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
清水白石0083 小时前
深入解析 LRU 缓存:从 `@lru_cache` 到手动实现的完整指南
java·python·spring·缓存
符哥20083 小时前
C++ 进阶知识点整理
java·开发语言·jvm
Sayuanni%33 小时前
初阶_多线程1(线程含义与关键属性)
java
程序媛徐师姐3 小时前
Java基于微信小程序的模拟考试系统,附源码+文档说明
java·微信小程序·java模拟考试系统小程序·模拟考试微信小程序·模拟考试系统小程序·模拟考试小程序·java模拟考试小程序
疯狂敲代码的老刘3 小时前
JDK 1.6到25 全版本网盘合集 (Windows + Mac + Linux)
java·linux·windows·macos·jdk
夕除3 小时前
js--15
java·jvm·spring
曾经的三心草3 小时前
redis-9-集群
java·redis·mybatis
sun03223 小时前
【架构基础】Spring中的PropertySourcesPlaceholderConfigurer介绍 (并非新知识,比较古老的一种使用方式)
java·spring·架构