tags:

views:

224

answers:

2

I have a repository class that is created in XML like so:

<bean id="stuffRepositoryTarget" class="my.stuff.RepositoryImpl">
 <!-- some params -->
</bean>

<bean id="stuffRepository" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" primary="true">
  <property name="target" ref="stuffRepositoryTarget" />
  <property name="transactionAttributes">
     <prop key="*">PROPAGATION_REQUIRED</prop>
  </property>
</bean>

And then I have a class that uses the repository like this:

@Autowired Repository repository;

It appears that the @Autowired annotation is referring to my Impl object and not my transaction interceptor. What am I doing wrong?

A: 

There may be confusion over which bean to inject... Add a qualifier to your bean reference with which you can specify the exact bean id that you wish to have injected.

Something like:

@Qualifier("stuffRepository")
@Autowired Repository repository;
Michael Wiles
+1  A: 

The most likely explanation is that the bean created by the TransactionProxyFactoryBean does not implement Repository.

Just checking, but Repository is an interface, right?

Another thing to try is to mark stuffRepositoryTarget with autowire-candidate="false", which will prevent it from being accidentally auto-wired.

skaffman
autowire-candidate="false" is a cool trick that I was not aware of. Thanks for the suggestion!
CaptainAwesomePants
It's only fair, you taught me an obscure fact about autowiring the other day :)
skaffman