当需要在某些Spring项目一启动,就执行某些操作时,需要实现改接口ApplicationListener,重写onApplicationEvent方法,将需要的预处理操作全部写在该方法中

当初始化完成WebApplicationContext后,spring容器一启动,就将执行预处理操作。

@Service
public class MyListener implements ApplicationListener<ContextRefreshedEvent>{
    Log logger = LogFactory.getLog(getClass());
    @Autowired
    private OfficeToPdfManager officeToPdfManager;
    @Autowired
    private OssService ossService;
    @Autowired
    private NoteManager noteManager;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){
            new Thread(officeToPdfManager).start();
            logger.info("**线程以启动");
            new Thread(ossService).start();
            logger.info("**线程以启动");
            new Thread(noteManager).start();
            logger.info("**启动");
    }
    }

}
如图所示:
Spring预处理
Spring预处理

相关文章:

  • 2022-12-23
  • 2021-04-30
  • 2021-06-04
  • 2021-08-03
  • 2022-03-07
  • 2022-02-09
  • 2021-10-30
猜你喜欢
  • 2021-08-12
  • 2021-07-03
  • 2022-02-13
相关资源
相似解决方案