Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

java - Transactional annotation error

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...