我有以下接口(interface)及其具体实现
public interface ReportGenerator {
void generateReport(ReportParameter reportParameter)
}
@Component
public class ConcreteReportGenerator {
@Override
@Transactional //**If concrete method is annotated transaction then bean not instantiating**
void generateReport(ReportParameter reportParameter) {
//Concrete Implementation
}
}
如果使用事务注释进行注释,则会出现以下错误
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type[...ConcreteReportGenerator ]
没有事务注释它工作正常。有人可以告诉我为什么它不启动事务注释。
最佳答案
@YairHarel:是的,我在另一个 bean 中连接 ConcereteReportGenerator。如果我按类加载 bean(如下所述),那么如果注释的具体方法是事务性的,它将失败:
applicationContext.getBean(ConcereteReportGenerator.class)
但是当我将其更改为按名称加载并将类型转换为接口(interface) (ReportGenerator) 时,它工作正常
(ReportGenerator) applicationContext.getBean("concereteReportGenerator")
下面的博客解释得很好。如果有人在寻找细节,请参阅第 5 点
http://www.baeldung.com/spring-nosuchbeandefinitionexception
关于java - 如果方法是事务性的,则 Spring 无法实例化 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35973917/