1. The purpose of this post
I would demo how to use java to solve this exception when @Autowire a property in spring.
- java 1.8+
- spring framework 4+
3. The Code
The object to be autowired:
The class is final because I don’t want others to override its methods. But when I use @Autowire and @Lazy in other classes like this:
Run the app, I got the above exceptions.
Because spring would use CGLIB to generate a subclass of the @Lazy class, and MyObject is final,so the exception occurred, I change the code like this:
Clean and re-run the app, everything works fine now.