java 事务注释错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39483059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transactional annotation error
提问by syv
When I put "@Transactional(readOnly=false)
" annotation in my Service class I get the following error
当我@Transactional(readOnly=false)
在 Service 类中放置“ ”注释时,出现以下错误
Description:
描述:
The bean 'studentService' could not be injected as a '
com.student.service.StudentServiceImpl
' because it is a JDK dynamic proxy that implements: com.student.service.StudentService
bean 'studentService' 无法作为 '
com.student.service.StudentServiceImpl
'注入,因为它是一个 JDK 动态代理,实现了:com.student.service.StudentService
Sample code:
示例代码:
@Service("studentService")
@Transactional(readOnly=false)
public class StudentServiceImpl implements StudentService {
}
public interface StudentService {
}
Action:
行动:
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting
proxyTargetClass=true
on@EnableAsync
and/or@EnableCaching
.Process finished with exit code 1
考虑将 bean 作为其接口之一注入,或通过设置
proxyTargetClass=true
on@EnableAsync
和/或强制使用基于 CGLib 的代理@EnableCaching
。进程以退出代码 1 结束
What is causing this?
这是什么原因造成的?
回答by gtiwari333
As SO already mentioned on the comment, the error occurs when you are trying to inject/autowire the implementation class instead of interface.
正如评论中已经提到的那样,当您尝试注入/自动装配实现类而不是接口时会发生错误。
The bean 'studentService' could not be injected as a 'com.student.service.StudentServiceImpl' because it is a JDK dynamic proxy that implements: com.student.service.StudentService
bean 'studentService' 不能作为 'com.student.service.StudentServiceImpl' 注入,因为它是一个 JDK 动态代理,实现了:com.student.service.StudentService
On the setup posted by SO,
在 SO 发布的设置中,
public class StudentServiceImpl implements StudentService {
}
public interface StudentService {
}
If you autowire the interface as below you won't get an error:
如果按如下方式自动连接接口,则不会出现错误:
@Autowired //or @Inject
StudentService studentService;
回答by mtbadi39
in spring boot projects, try to add :
在 spring boot 项目中,尝试添加:
spring.aop.proxy-target-class=true
to your application.properties
到您的 application.properties
OR
或者
@EnableAspectJAutoProxy(proxyTargetClass = true)
to your spring boot entry point.
到您的 Spring Boot 入口点。
回答by Rohan
In your application class file add this:
在您的应用程序类文件中添加以下内容:
@SpringBootApplication
@EnableCaching(proxyTargetClass = true)