我正在使用 java config@ComponentScan
来初始化我的 bean 并@EnableAspectJAutoProxy(proxyTargetClass=true)
使用 cglib 代理。
在这个项目中,我们有很多生成的服务在它们之间使用@Autowired
. 它工作得很好。
但是,对于我添加的其中一些服务@Async
(我也添加@EnableAsync(proxyTargetClass = true)
到我的@Configuration
课程中)。
之后,我得到:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'ConversationUserLocalService': Bean with name 'ConversationUserLocalService' has been injected into other beans [ConversationUserHistoryLocalService] i
n its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'a
llowEagerInit' flag turned off, for example.
我猜这是因为 Spring@Async
在 AOP 创建代理之前使用方法注入服务。这可能是问题吗?我应该如何修复它?
为了试图澄清我的问题,假设我有:
@服务 A、B 和 C;
A 自动接线 B 和 C,B 自动接线 A 和 C,C 自动接线 A 和 B;
C 有一个标记为@Async 的方法。
Spring初始化applicationContext的时候,尝试初始化A,但是需要B&C,所以初始化了它们。但毕竟,AOP 尝试创建 C 的代理(因为 @Async),然后它检测到将 C 自动连接到 B 和 A 与 C 的代理不同,所以它失败了。
我希望这可以解释更多正在发生的事情。