spring autowired aop circular dependency

后端 未结 4 401
萌比男神i
萌比男神i 2021-01-31 09:07

I\'m using java config with @ComponentScanin order to initialize my beans and @EnableAspectJAutoProxy(proxyTargetClass=true)to use cglib proxies.

相关标签:
4条回答
  • 2021-01-31 09:13

    Finally I sorted it out using @Lazyon 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.

    0 讨论(0)
  • 2021-01-31 09:14

    I have same issue and I solved this issue:

    1. I identified which @Autowired property is reason for circular dependency.

      Eg:

      @Autowired
      private TestService testService;
      

      (Tips to identified just try to comment and find out which property is reason to break the application)

    2. Once identified just use @Lazy on top of this @Autowired variable.

      Eg :

      @Lazy
      @Autowired
      private TestService testService;
      

      And Application worked smoothly.

    0 讨论(0)
  • 2021-01-31 09:21

    I managed to fix a similar issue by adding @Qualifier together with @Autowire, for example:

    @Autowired
    @Qualifier("publisher")
    private Publisher publisher;
    
    0 讨论(0)
  • 2021-01-31 09:33

    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

    0 讨论(0)
提交回复
热议问题