交易注释错误

syv*_*syv 14 java dependency-injection spring-boot

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

描述:

bean"studentService"无法作为' com.student.service.StudentServiceImpl' 注入,因为它是一个实现的JDK动态代理:com.student.service.StudentService

示例代码:

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

}

public interface StudentService {

}
Run Code Online (Sandbox Code Playgroud)

行动:

考虑将bean注入其接口之一或通过设置proxyTargetClass=trueon @EnableAsync和/或强制使用基于CGLib的代理@EnableCaching.

进程以退出代码1结束

是什么造成的?

gti*_*333 17

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

bean"studentService"无法作为"com.student.service.StudentServiceImpl"注入,因为它是一个实现的JDK动态代理:com.student.service.StudentService

在SO发布的设置上,

public class StudentServiceImpl implements StudentService {
}

public interface StudentService {
}
Run Code Online (Sandbox Code Playgroud)

如果您按如下所示自动连接界面,则不会出现错误:

@Autowired //or @Inject
StudentService studentService;
Run Code Online (Sandbox Code Playgroud)

  • @Nurlan检查限定符注释 (2认同)

小智 16

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

spring.aop.proxy-target-class=true
Run Code Online (Sandbox Code Playgroud)

到您的application.properties

要么

@EnableAspectJAutoProxy(proxyTargetClass = true)
Run Code Online (Sandbox Code Playgroud)

到你的春季启动入口点.

  • 从 \@EnableAspectJAutoProxy 开始,您可以将 proxyTargetClass = true 添加到 \@EnableAsync 和/或 \@EnableCaching 中的任何一个 (2认同)