No qualifying bean of type 'concert.PerformanceImp' available

后端 未结 2 1039
盖世英雄少女心
盖世英雄少女心 2021-01-27 07:56

I am still a beginner in Spring Framework so I tried to code a program about \"introduction\" in Spring AOP but I am facing an error while compiling. Please find below the class

相关标签:
2条回答
  • 2021-01-27 08:19

    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.

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

    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.

    So you have two options:

    1. Take the interface when trying to get the bean from the ApplicationContext.
    2. Enable AOP to target concrete classes instead.

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

    @EnableAspectJAutoProxy(proxyTargetClass = true)
    
    0 讨论(0)
  • 2021-01-27 08:26

    Try:

    Performance pi = context.getBean("performanceImp", Performance.class);
    

    instead of:

    PerformanceImp pi = (PerformanceImp) context.getBean(PerformanceImp.class);
    
    0 讨论(0)
提交回复
热议问题