spring ioc injecting concrete implementation of interface to test

南楼画角 提交于 2019-12-04 19:27:11
  1. Check if the exception is about the beans you think it is. Because the names don't match
  2. Even if 1 is fixed, it is preferable to autowire by interface, not by concrete implementation. Most often (I can' know if this is true in your case), concrete implementations are proxied by spring (for transaction support, for example), and can be injected by interface only. And since you have two implementations of one interface, you have to provide a name, and either use @Autowired + @Qualifier, or use @Resource(name="") to inject what you want. And there is nothing wrong with this.

Ok, today i found a way to make this work afterall. Seems like the Spring-AOP-Autoproxy was to blame for this not to work.

Currently I am using AJDT in Eclipse for compile time weaving (CTW) and the TransactionAnnotationAspect. I also removed the aspectjweaver and cglib dependency in my project, so autoproxy is not even supported anymore, after removing the following config part from applicationcontext:

<aop:pointcut id="serviceOperation" expression="execution(* some.package..*(..)) &amp;&amp; @target(org.springframework.stereotype.Service)" />
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="serviceTxAdvice" />
</aop:config>
<tx:advice id="serviceTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" read-only="true" propagation="SUPPORTS" />
    </tx:attributes>
</tx:advice>

And that was also the reason why @Configurable didn't work for me...

Adding the following line worked for me:

<aop:aspectj-autoproxy proxy-target-class="true"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!