如何避免 Spring 6 非致命的“TargetSource 无法确定目标类”堆栈跟踪输出?

问题描述 投票:0回答:1

我们有一个 Spring 应用程序,它使用

BeanFactoryPostProcessor
定义了一些 bean。使用 Spring 6 (Spring Boot 3),在启动时会打印非致命的
INFO
级别堆栈跟踪,如下所示:

2023-10-19T16:15:42.223+02:00  INFO 18756 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : FactoryBean threw exception from getObjectType, despite the contract saying that it should return null if the type of its object cannot be determined yet

org.springframework.aop.framework.AopConfigException: TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.
        at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:65) ~[spring-aop-6.0.12.jar:6.0.12]
...

相同的代码在 Spring 5 (Spring Boot 2) 中运行良好(没有输出堆栈跟踪)。该应用程序的作用基本上是

  1. 已配置
    ApplicationContextInitializer
  2. 初始化器添加了一个
    BeanFactoryPostProcessor
  3. 处理器定义代理服务
  4. 使用服务;在 Spring 5 中没有显示错误,在 Spring 6 中出现堆栈跟踪 - 应用程序在两种情况下都可以工作

此处显示了该问题的一个小演示:https://github.com/smurf667/test-spring-boot3-aop-issue

我们是否做错了什么,或者这是 Spring 6 中的问题/回归?

spring spring-boot spring-aop
1个回答
0
投票

这不是一个明确的答案,但信息太多,无法评论。

我在两个 Spring Boot 版本中进行了调试,我发现异常被捕获并记录在here

Spring 5.3.29 (Boot 2.7.15) 和 Spring 6.0.12 (Boot 3.1.4) 之间的区别在于,字段

AdvisedSupport.targetSource
在旧 Spring 版本中比在新版本中设置得更早,即当目标源在方法
DefaultAopProxyFactory.createAopProxy
中进行了检查,在旧的Spring版本中已经设置了,但在新版本中仍然是
EmptyTargetSource
实例,导致抛出异常。有趣的细节:错误日志说在那里抛出异常违反了约定,这就是它被捕获并记录的原因。

我认为,这值得创建一个 Spring 问题,要求澄清这是否被认为是一个错误,仅仅是一个烦恼,或者问题位于计算机前面,以某种方式使用这些上下文初始化程序和 bean 工厂后处理器是错误的或非规范方式。我根本不是 Spring 用户,对这些东西的了解为零。我只是对这个问题感兴趣,因为我是一个 AOP 专家。

© www.soinside.com 2019 - 2024. All rights reserved.