Spring @Async Not Working

后端 未结 11 746
北荒
北荒 2020-12-08 06:30

An @Async method in a @Service-annotated class is not being called asynchronously - it\'s blocking the thread.

I\'ve got

11条回答
  •  没有蜡笔的小新
    2020-12-08 06:59

    @Async can not be used in conjunction with lifecycle callbacks such as @PostConstruct. To asynchonously initialize Spring beans you currently have to use a separate initializing Spring bean that invokes the @Async annotated method on the target then.

    public class SampleBeanImpl implements SampleBean {
    
      @Async
      void doSomething() { … }
    }
    
    
    public class SampleBeanInititalizer {
    
      private final SampleBean bean;
    
      public SampleBeanInitializer(SampleBean bean) {
        this.bean = bean;
      }
    
      @PostConstruct
      public void initialize() {
        bean.doSomething();
      }
    }
    

    source

提交回复
热议问题