作用:
spring所有Bean生成之后,加载一些数据和执行一些应用的初始化
使用
@Order(value = 1) // 多个实现时定义执行顺序 @Component public class ApplicationInit implements CommandLineRunner, ApplicationListener<ApplicationEvent> { private static Log logger = LogFactory.getLog(ApplicationInit.class); @Autowired private ActiveMQTask activeMQTask; private boolean openCVLoad; public static String openCVXmlPath; @Autowired private JmsConsumerTask jmsConsumerTask; @Autowired private DefaultMessageListener messageListener; @Override public void run(String... args) { System.out.println("------------------------ Spring初始化完毕 ------------------------"); logger.error("---" + Arrays.toString(args) + "---"); this.applicationStarted(); //spring启动后执行的自定义任务:开启activityMQ队列 } @Override public void onApplicationEvent(ApplicationEvent event) { if (logger.isDebugEnabled()) { logger.debug("event:" + event.getClass().getName()); } } private void applicationStarted() { System.out.println("> 启动ActiveMQ[启动服务]"); activeMQTask.start(); } private void appStop() { logger.error("----------------------------Tomcat开始运行关闭----------------------------"); }
推荐:CommandLineRunner https://blog.csdn.net/lk142500/article/details/90270592
ApplicationListener https://blog.csdn.net/liyantianmin/article/details/81017960