Tejus's Programming and startup blog

  • Archive
  • RSS
  • Ask me anything

@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:

http://gist.github.com/1568484

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:

http://gist.github.com/1568539

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:

http://gist.github.com/1568553

Basically, one needs to change the type back to an interface, then use named services and the @Qualifer annotation to inject the desired dependency.

  • 1 month ago
  • Comments
  • Permalink
  • Share
    Tweet

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

Avatar

I'm Tejus Parikh and this is my blog about programming, startups, and other technology related topics.

The vast majority of the posts are about Java or Ruby code, with a few about startups or gadgets thrown in for color.

You can learn more about me on Github and my personal homepage.

Me, Elsewhere

  • @vi_jedi on Twitter
  • Facebook Profile
  • Linkedin Profile
  • vijedi on github
  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr