1

我正在使用 Spring-boot 创建一个多模块 Maven 应用程序。我在一个模块中有服务层,在另一个模块中有 Web 层。我无法启动应用程序,出现以下错误:

The bean 'Service' could not be injected as a 'Service' because it is a JDK dynamic proxy that implements:


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

尝试使用注释配置类

@EnableAsync(proxy-target-class=true)
@EnableCaching(proxy-target-class=true)
@EnableTransactionManagement(proxy-target-class=true)
@EnableAspectJAutoProxy(proxy-target-class=true)

一旦我添加了@PreAuthorize 或@Transactional 类型的任何注释,应用程序就会停止工作。

该服务没有实现任何接口,我希望 Spring 使用 CGLib 代理进行自动连接,从跟踪日志中,我看到以下内容:

* Adding transactional method 'Service.remove' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
* Creating implicit proxy for bean 'Service' with 0 common interceptors and 3 specific interceptors.
* Creating CGLIB proxy: SingletonTargetSource for target object [Service@17765082]
* Adding transactional method 'Service$$EnhancerBySpringCGLIB$$6f15732.remove' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
* Creating implicit proxy for bean 'Service' with 0 common interceptors and 2 specific interceptors
* Creating JDK dynamic proxy: SingletonTargetSource for target object [Service$$EnhancerBySpringCGLIB$$6f15732@32142479]

自动接线的例外是:

Bean named 'locationService' is expected to be of type 'Service' but was actually of type 'com.sun.proxy.$Proxy202'

这是否意味着 Spring 将我的服务注册为 CGLib 代理和 JDK 代理?如果是这样,我怎么能强制它只使用 CGLib 代理?

4

1 回答 1

0

问题凭直觉解决。Spring AOP 正常工作,并按预期使用 CGLib 代理。问题是一个覆盖代理的依赖项。

我使用 JavaMelody 进行监控,它正在为 CGLib 创建的代理创建 JDK 代理。

尽管如此,删除 Java Melody,或将其配置为与 CGLib 一起使用,如此处所述,JavaMelody-ISSUE-502解决了问题,应用程序按预期工作。

于 2019-11-09T22:13:40.420 回答