使用tx:annotation-driven时,Spring 2.5 SpringJUnit4 ClassRunner无法自动连线

使用tx:annotation-driven时,Spring 2.5 SpringJUnit4 ClassRunner无法自动连线,spring,autowired,Spring,Autowired,测试代码 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:transactional/batch/context.xml" }) public class TransactionTest { @Autowired TestBatch testBatch; 案例1: TestBatch已成功自动连接到TransactionTest context.xml <cont

测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:transactional/batch/context.xml" })
public class TransactionTest {
@Autowired
TestBatch testBatch;
案例1: TestBatch已成功自动连接到TransactionTest

context.xml

<context:annotation-config />
<bean id="testBatch" class="transactional.batch.TestBatch"/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<context:annotation-config />
<bean id="testBatch" class="transactional.batch.TestBatch"/>
<tx:annotation-driven transaction-manager="transactionManager"/>

案例2: 但这个案子失败了

context.xml

<context:annotation-config />
<bean id="testBatch" class="transactional.batch.TestBatch"/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<context:annotation-config />
<bean id="testBatch" class="transactional.batch.TestBatch"/>
<tx:annotation-driven transaction-manager="transactionManager"/>

例外情况

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: transactional.batch.TestBatch transactional.batch.TransactionTest.testBatch; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [transactional.batch.TestBatch] is defined: Unsatisfied dependency of type [class transactional.batch.TestBatch]: expected at least 1 matching bean at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435) at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240) ... 19 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [transactional.batch.TestBatch] is defined: Unsatisfied dependency of type [class transactional.batch.TestBatch]: expected at least 1 matching bean at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412) ... 21 more 原因:org.springframework.beans.factory.BeanCreationException:无法自动关联字段:transactional.batch.TestBatch transactional.batch.TransactionTest.TestBatch;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义类型为[transactional.batch.TestBatch]的唯一bean:类型为[class transactional.batch.TestBatch]的未满足的依赖项:应至少有1个匹配bean 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.inject(AutoWiredNotationBeanPostProcessor.java:435) 位于org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105) 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.postProcessAfterInstantiation(AutoWiredNotationBeanPostProcessor.java:240) ... 还有19个 原因:org.springframework.beans.factory.NoSuchBean定义异常:未定义类型为[transactional.batch.TestBatch]的唯一bean:类型为[class transactional.batch.TestBatch]的未满足依赖项:应至少有1个匹配bean 位于org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613) 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.inject(AutoWiredNotationBeanPostProcessor.java:412) ... 还有21个 唯一的区别是
tx:annotation-driven
代理目标类
属性


为什么
tx:annotation-driven
控制自动布线

我的猜测是,
TestBatch
实现了一个接口,并且在一些方法上有
@Transactional

将为
TestBatch
生成事务代理对象。默认情况下,如果
TestBatch
实现了任何接口,那么这个代理对象将实现这些接口,但不会扩展
TestBatch。如果使用
proxyTargetClass
,则生成的代理对象将改为子类
TestBatch`


由于单元测试要求对象的类型为
TestBatch
,这意味着您必须在配置中使用
proxyTargetClass
。或者,更改单元测试,以便使用
TestBatch
的一个接口,而不是直接使用
TestBatch
类型。

在我看来,这似乎不是“tx:annotation-driven-control-the-auto-wire”。但是,基于堆栈,自动代理类具有相同的id
没有类型为[transactional.batch.TestBatch]
的唯一bean。你为什么不试试via?