TheClass class implements TheInterf...">

Spring Class autowiring vs. interface?

I have this Spring config:

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

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

 @Autowired TheInterface x; @Autowired TheClass y; 

TheInterface works, but TheClass auto-detection fails. Spring gives me a NoSuchBeanDefinitionException for the class.

Why can you connect an interface, not a class?

+39
java spring dependency-injection autowired
Mar 05 '10 at 14:34
source share
1 answer

Typically, both will work, you can autwire interfaces or classes.

There is probably an auto-proxy generator in your context that transfers your boo bean to the generated proxy object. This proxy object will implement TheInterface , but TheClass will TheClass . When using autoproxies, you need to program the interface, not the implementation.

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

+52
Mar 05
source share



All Articles