Transactional annotation error

拜拜、爱过 提交于 2019-12-18 18:59:54

问题


When I put "@Transactional(readOnly=false)" annotation in my Service class I get the following error

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

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

What is causing this?


回答1:


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

On the setup posted by 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;



回答2:


in spring boot projects, try to add :

spring.aop.proxy-target-class=true

to your application.properties

OR

@EnableAspectJAutoProxy(proxyTargetClass = true)

to your spring boot entry point.




回答3:


In your application class file add this:

@SpringBootApplication
@EnableCaching(proxyTargetClass = true)


来源:https://stackoverflow.com/questions/39483059/transactional-annotation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!