17
votes

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?

4
If you are trying to build project using JRE then can you please change it to JDK? Do you have all aspect related jars on your classpath? - Nimesh
It's my mistake, I have autowired the implementation class insted of Interface. - syv
You could answer your own question with that answer because I ran into the same thing and this helped me resolve it. Thanks! - emilyk

4 Answers

26
votes

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;
17
votes

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.

1
votes

In your application class file add this:

@SpringBootApplication
@EnableCaching(proxyTargetClass = true)
0
votes

I had similar problem and resolved in this way