@Autowire with AOP Proxy Classes in Spring
This is an issue that gets me once every blue moon. Lets say that I have two classes DependencyClass and MainClass:
DependencyClass makes use of Spring’s @Transactional support, which is provided through AOP Proxies. This code works when I fire it up. However, if I add another implementation, and want to change the code to the following:
I get this nasty on the application startup:
NoSuchBeanDefinitionException: No matching bean of type [DependencyClass] found for dependency
The reason for this is simple. Within the Spring container, the concrete class DependencyClass has been replaced by a proxy. Therefore, as far as the system is concerned, there is no matching bean of type DependencyClass.
Inevitably, when this occurs, I spend too much time looking to other issues with my application context setup. However, the solution is easy and straightforward, once you realize what the problem is:
Basically, one needs to change the type back to an interface, then use named services and the @Qualifer annotation to inject the desired dependency.