且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有可用的'concert.PerformanceImp'类型的合格Bean

更新时间:2023-08-21 12:13:10

默认情况下,您无法访问实现(PerformanceImp),因为您启用了AOP,它将设置为目标接口而不是实现.如果您删除EnableAspectJAutoProxy,您会看到代码可以正常工作.

You cannot access the implementation (PerformanceImp) by default, because you enabled AOP, which sets to target interfaces instead of implementation. If you would remove EnableAspectJAutoProxy, you would see the code would work fine.

要进一步了解AOP定位的工作原理,请查看以下

To understand a bit more about how AOP targeting works, take a look at this Spring Documentation

Spring AOP也可以使用CGLIB代理.这是代理的必要条件 类而不是接口.如果业务是默认情况下,则使用CGLIB 对象未实现接口.因为这是一个好习惯 编程接口而不是类;正常的商务舱 将实现一个或多个业务接口.有可能 在您需要的那些(非常罕见)情况下,强制使用CGLIB 告知未在接口或您所在位置声明的方法 需要将代理对象作为具体类型传递给方法.

Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes; business classes normally will implement one or more business interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface, or where you need to pass a proxied object to a method as a concrete type.

因此,您有两个选择:

  1. 尝试从ApplicationContext获取Bean时,请使用接口.
  2. 启用AOP来定位具体类.
  1. Take the interface when trying to get the bean from the ApplicationContext.
  2. Enable AOP to target concrete classes instead.

要执行第2点,请按如下所示修改注释:

To do this point #2, modify your annotation as follows:

@EnableAspectJAutoProxy(proxyTargetClass = true)