使用@Transactional
注释实现类中的方法会引发此异常。
// Interface
public interface JobManager {
public void process(CommandLine parameters);
}
// Implementing class
public class UserJobManager implements JobManager{
@Transactional
@Override
public void process(CommandLine line) {
// Stuff here
}
}
Spring 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="benchmarkManager" class="com.test.userjobmanager.UserJobManager"/>
<tx:annotation-driven transaction-manager="transactionManager" order="10"/>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="readDataSource"/>
</bean>
<!-- other beans here -->
</beans>
当我访问 bean 时:
JobManager jobManager = ac.getBean(UserJobManager.class);
我有以下堆栈跟踪:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.test.userjobmanager.UserJobManager] is defined: expected single bean but found 0: at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:269) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
如果我删除@Transactional
,它就可以正常工作。
我不确定这里出了什么问题。
最佳答案
也许您正在使用创建合成类的代理技术。也就是说,您的事务bean 的真正类可能不再是UserJobManager
。我建议改为按名称访问 bean:
JobManager jobManager = ac.getBean("benchmarkManager", JobManager.class)
关于java - 使用@Transactional注释方法会导致 "No unique bean of type is defined: expected single bean but found 0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654860/