Spring预处理

时间:2023-03-08 16:30:39

当需要在某些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预处理