java - 如果方法是事务性的,则 Spring 无法实例化 bean

标签 java spring transactions autowired spring-bean

我有以下接口(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/

相关文章:

spring - javax.validation.ConstraintDefinitionException : HV000074 in Spring MVC

java - 在 Spring 4 中用带注释的 Controller 替换 BaseCommandController 和 SimpleFormController

java - Grails,使用 withTransaction 插入大量数据会导致 OutOfMemoryError

返回 PHP PDO 错误消息,但回滚不起作用

java - 无法从外部连接到 Jetty 中嵌入的 ActiveMQ 代理

java - 调用函数以显示/隐藏来自另一个类的 ImageView

java - 将文件中的整数添加到数组

Java重载方法选择

java - Spring在方法上的@Profile是一个好习惯吗

iOS CoreData 提交子事务但取消主事务