spring autowired aop circular dependency
Finally I sorted it out using @Lazy
on services (with methods annotated with @Async
), and also, where they were autowired.This way I guess Spring only initialize and autowires those services when they're required instead of on application context initialization.
I have same issue and I solved this issue:
I identified which
@Autowired
property is reason for circular dependency.Eg:
@Autowiredprivate TestService testService;
(Tips to identified just try to comment and find out which property is reason to break the application)
Once identified just use
@Lazy
on top of this@Autowired
variable.Eg :
@Lazy@Autowiredprivate TestService testService;
And Application worked smoothly.
AsyncConfigurer configuration classes get initialized early in the application context bootstrap. If you need any dependencies on other beans there, make sure to declare them @Lazy
as far as possible in order to let them go through other post-processors as well.
Reference JavaDoc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html