尝试使用@Async In Component

时间:2018-07-11 10:38:26

标签: spring hibernate asynchronous

当Im使用@Component注释而不使用任何@Autowired时,我正在尝试使用springs @Async处理;当Im在组件中使用@Autowired来获取其他bean时,我正在尝试使用

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'asyncComp': Bean with name 'asyncComp' has been injected into other beans [containerService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

测试类

TestClass{
   static AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

public static void main( String[] args ) throws SchedulerException
    {
      ContainerServiceLocal containerService = (ContainerServiceLocal) context.getBean("containerService");
        containerService.printAsync();
    }
}

集装箱服务

@Service("containerService")
@Transactional
public class ContainerService implements ContainerServiceLocal {
   @Autowired
    AsyncCompLocal comp;

   public void printAsync(){
     comp.backgroudProcessing();
    }
}

AsyncComp

@Component
public class AsyncComp implements AsyncCompLocal{


    @Autowired
    ContainerServiceLocal containerService;

    @Async
    @Override
    public void backgroudProcessing() {
        for(int i=0;i<10;i++){
            System.out.println("Backgroud processing");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

AsyncCompLocal

public interface AsyncCompLocal {
    public void backgroudProcessing();
}

0 个答案:

没有答案