【问题标题】:Transactional annotation error事务注释错误
【发布时间】:2016-09-14 05:29:16
【问题描述】:

当我在我的服务类中添加“@Transactional(readOnly=false)”注释时,我收到以下错误

说明:

无法将 bean 'studentService' 作为 'com.student.service.StudentServiceImpl' 因为它是 JDK 动态的 实现的代理:com.student.service.StudentService

示例代码:

@Service("studentService")
@Transactional(readOnly=false)
public class StudentServiceImpl implements StudentService {

}

public interface StudentService {

}

行动:

考虑将 bean 作为其接口之一注入,或通过在 @EnableAsync 和/或 @EnableCaching 上设置 proxyTargetClass=true 来强制使用基于 CGLib 的代理。

进程以退出代码 1 结束

是什么原因造成的?

【问题讨论】:

  • 如果您尝试使用 JRE 构建项目,那么您可以将其更改为 JDK 吗?您的类路径中是否有所有方面相关的 jar?
  • 我错了,我已经自动装配了Interface的实现类。
  • 你可以用那个答案回答你自己的问题,因为我遇到了同样的事情,这帮助我解决了它。谢谢!

标签: java spring-boot dependency-injection


【解决方案1】:

正如评论中已经提到的那样,当您尝试注入/自动装配实现类而不是接口时会发生错误。

无法将 bean 'studentService' 作为 'com.student.service.StudentServiceImpl' 因为它是 JDK 动态的 实现的代理:com.student.service.StudentService

关于 SO 发布的设置,

public class StudentServiceImpl implements StudentService {
}

public interface StudentService {
}

如果你按照下面的方式自动连接接口,你不会得到错误:

@Autowired //or @Inject
StudentService studentService;

【讨论】:

  • 如果接口的实现有很多,spring怎么知道我要注入哪一个呢?
  • @Nurlan 检查限定符注释
【解决方案2】:

在spring boot项目中,尝试添加:

spring.aop.proxy-target-class=true

到您的 application.properties

@EnableAspectJAutoProxy(proxyTargetClass = true)

到你的 Spring Boot 入口点。

【讨论】:

  • Aprt 来自 \@EnableAspectJAutoProxy,您可以将 proxyTargetClass = true 添加到 \@EnableAsync 和/或 \@EnableCaching 中的任何一个
  • @AbhishekChatterjee 这实际上更好,因为@EnableAspectJAutoProxy 似乎需要额外的依赖。 @EnableCaching 也可以放在一个依赖项中,所以被依赖者不会错过它。
【解决方案3】:

在您的应用程序类文件中添加:

@SpringBootApplication
@EnableCaching(proxyTargetClass = true)

【讨论】:

    【解决方案4】:

    我有类似的问题,并以this 方式解决

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 2015-03-23
      • 2014-06-12
      • 2011-01-23
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多