网站建设企业站模板,自学小程序开发,静态网站开发一体化课程,谷德设计网官网首页入口目录 介绍
spirng启动后启动某方法 介绍
ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现#xff0c;内部实现原理是观察者设计模式#xff0c;设计初衷也是为了系统业务逻辑之间的解耦#xff0c;提高可扩展性以及可维护性。事件发布者并不需…目录 介绍
spirng启动后启动某方法 介绍
ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现内部实现原理是观察者设计模式设计初衷也是为了系统业务逻辑之间的解耦提高可扩展性以及可维护性。事件发布者并不需要考虑谁去监听监听具体的实现内容是什么发布者的工作只是为了发布事件而已。
Spring提供的内置事件
ContextRefreshedEvent容器刷新事件 ContextStartedEvent容器启动事件 ContextStoppedEvent容器停止事件 ContextClosedEvent容器关闭事件
spirng启动后启动某方法
package com.csc.coms.modules.job.util;import com.csc.coms.modules.task.service.TaskJobService;
import com.csc.coms.modules.taskconf.entity.TaskConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;Component
Slf4j
public class InitJob implements ApplicationListenerContextRefreshedEvent {Autowiredprivate TaskJobService taskJobService;Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {try {init(contextRefreshedEvent);} catch (Exception e) {log.error(InitJob启动失败{}, e);}}private void init(ContextRefreshedEvent contextRefreshedEvent) {System.out.println(InitJob定时任务------begin-------------);TaskConf taskConf new TaskConf();taskJobService.startTaskJobHandle(taskConf);System.out.println(InitJob定时任务-------end------------------);}
}