BeanPostProcessor 和 BeanFactoryPostProcessor 能做什么、会在哪里出问题?返回代理对象之后按具体类注入为什么失败?多个 BPP 谁先谁后?为什么 BPP 依赖的 Bean 会“不被所有 BPP 处理”?为什么 BFPP 的 @Bean 方法必须 static?这篇在 Spring Framework 7.0.9 上逐项验证。
ByIface 拿到:$Proxy10 调用结果 [包装]hi
getBean("svc") 的类型:$Proxy10,是 SvcImpl 的实例?false
异常 UnsatisfiedDependencyException ← BeanNotOfRequiredTypeException: Bean named 'svc' is expected to be of type 'demo.Main$SvcImpl' but was actually of type 'jdk.proxy2.$Proxy10'BPP 返回的对象会替换原 Bean;返回 JDK 代理后,按具体类注入会失败。
顺序:PriorityOrdered(5) → PriorityOrdered(10) → Ordered(0) → Ordered(1) → plain1(无顺序,最先注册) → plain2(无顺序,后注册)
PriorityOrdered、Ordered、无顺序三组,组内按 getOrder 升序,无顺序的按注册顺序。
LOG WARN PostProcessorRegistrationDelegate$BeanPostProcessorChecker: Bean 'main.EarlyConf' of type [demo.Main$EarlyConf$$SpringCGLIB$$0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying).… 构造 Service LOG WARN PostProcessorRegistrationDelegate$BeanPostProcessorChecker: Bean 'service' of type [demo.Main$Service] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eag… 构造 Early(BPP,构造器依赖 Service) Late(后注册的 BPP)看到了 plainBean
BPP 依赖的 Bean 在 BPP 注册阶段就被创建,后注册的 BPP 处理不到,Spring 打 WARN 提示。
LOG INFO ConfigurationClassEnhancer: @Bean method NonStatic.bfpp is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource, and… 非 static 的 BFPP @Bean 方法被调用时,配置类里的 @Autowired Environment 字段 = null static 的 BFPP @Bean 方法被调用(静态方法不需要先创建配置类) 配置类被创建后 @Autowired Environment 字段 = 已注入 BFPP 要在其它 Bean 之前就拿到实例,非 static 的 @Bean 方法需要先创建它所在的配置类,这时 BeanPostProcessor 还没注册,@Autowired 之类的注解处理不了;所以 BFPP 和 BPP 的 @Bean 方法都应该写成 static
非 static 的 BFPP @Bean 方法需要先创建配置类,这时注解处理还没有就绪。