Skip to content

Instantly share code, notes, and snippets.

@rponte
Created November 1, 2011 23:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rponte/1332286 to your computer and use it in GitHub Desktop.
Save rponte/1332286 to your computer and use it in GitHub Desktop.
Spring Advice: autowiring interface types rather than class types
There's another good reason to autowire interface types rather than class types, which is that if Spring needs
to generate a proxy object around the bean before injecting it, then if the bean's class defines any
interfaces, then the proxy will implement those interfaces, and will not be type-compatible with the bean class
itself. If you then try and autowire that bean by class type, it will fail. The easiest way to avoid this
annoying scenario is to always autowire by interface type, that way it will lways work as you expect.
http://stackoverflow.com/questions/5288153/using-spring-to-wire-directly-a-concrete-class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment