43

I have this Spring config:

<bean id="boo" class="com.x.TheClass"/>

The class TheClass implements TheInterface. Then I have this (hypothetical) Java code:

@Autowired
TheInterface x;

@Autowired
TheClass y;

The autowiring of TheInterface works but the autowiring of TheClass fails. Spring gives me a NoSuchBeanDefinitionException for the class.

Why can you wire the interface and not the class?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • 6
    Is there anything special about this class, like it being 'final', or having other instrumentation, like @Transactional, on it. You might be either missing an instrumentation lib, like CGLIB, or trying to create a subclass proxy on a final class. – ptomli Mar 05 '10 at 14:53

1 Answers1

57

Normally, both will work, you can autowire interfaces or classes.

There's probably an autoproxy generator somewhere in your context, which is wrapping your boo bean in a generated proxy object. This proxy object will implement TheInterface, but will not be a TheClass. When using autoproxies, you need to program to the interface, not the implementation.

The likely candidate is transactional proxies - are you using Spring transactions, using AspectJ or @Transactional?

Péter Török
  • 114,404
  • 31
  • 268
  • 329
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 5
    @Marcus: That's the problem then. If you use `@Transactional` and ``, you cannot cast the bean to `MyClass`, you have to use the interface. – skaffman Mar 05 '10 at 14:59
  • 29
    I know this is a bit old, but to add a bit here... You don't _have_ to use the interface, but in order to autowire the class directly, you need to modify the `` configuration and add `proxy-target-class="true"` (this defaults to false). This lets you auto-wire directly to the class. Note that there can be weird side effects, e.g. I had used some reflection to find the parameterized type for a generic base class. The inheritance is changed due to the proxy class, so I had to take that into account. You can still wire by interface with `proxy-target-class="true"`. – Spanky Quigman Oct 13 '11 at 14:36