从Spring MVC到SpringBoot 2:不能将bean'xyz'作为'com..Abc'注入,因为它是实现以下功能的JDK动态代理:

时间:2019-02-07 23:21:38

标签: java spring-boot spring-mvc autowired dynamic-proxy

我在Spring Web MVC中有一个正在工作的项目。 类“ A”是抽象类。 “ B”类扩展了A,“ C”类扩展了B。 C类具有以下注释;

@Component
@Primary

一切工作一直很好,直到最近,我们决定转到SpringBoot 2。 我们已经将项目迁移到SpringBoot 2,添加了必需的依赖项。 现在,当我运行项目时,出现此错误;

Bean named 'c' is expected to be of type 'com..B' but was actually of type 'com.sun.proxy.$Proxy132'

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'c' could not be injected as a 'com..B' 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.

我已经进行了一些挖掘,发现了一个在线解决方案,对我不起作用。*; **使用任何这些注释;

@EnableAspectJAutoProxy(proxyTargetClass=true)
@EnableAsync(proxyTargetClass=true)
@EnableCaching.(proxyTargetClass=true)

P.S。 我有如下配置类;

EnableAspectJAutoProxy(proxyTargetClass=true)
@Configuration("mySpringConfig")
@ComponentScan(basePackages = { "com.allpackages" }, excludeFilters = {someExcludeFilters})
@Conditional(SomeApiSpringConfigCondition.class)
public class SomeCoreSpringConfig extends ApiWebConfiguration{
}

此外,SpringBoot入口点如下所示;

@SpringBootApplication(exclude = { SomeClasses})
@EnableWebMvc
public class AppInitializer {

    public static void main(String[] args) {

        SpringApplication.run(AppInitializer.class, args);

    }

}

谢谢!

1 个答案:

答案 0 :(得分:0)

经过大量的挖掘,通过在“ C”类中添加以下内容来使其正常工作。 @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)

所以我的班级“ C”看起来像这样;

@Component
@Primary
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
class C{}

但是,现在其他班级都出现了错误。所以我要说,这不是一个可持续的解决方案!