Spring中的监听器(及对应的应用)


监听器

  1. spring框架的启动入口 ContextLoaderListener
  2. 作用:在启动Web 容器时,自动装配Spring applicationContext.xml 的配置信息。

因为它实现了ServletContextListener 这个接口,在web.xml 配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener 中关联了ContextLoader 这个类,所以整个加载配置过程由ContextLoader 来完成。

借用监听器来作为Spring项目启东时来初始化数据

Spring的事件驱动模型由三部分组成
- 事件: ApplicationEvent,继承自JDK的EventObject,所有事件都要继承它,也就是被观察者
- 事件发布者: ApplicationEventPublisher及ApplicationEventMulticaster接口,使用这个接口,就可以发布事件了
- 事件监听者: ApplicationListener,继承JDK的EventListener,所有监听者都继承它,也就是我们所说的观察者,当然我们也可以使用注解 @EventListener,效果是一样的

事件
在Spring框架中,默认对ApplicationEvent事件提供了如下支持:
- ContextStartedEvent:ApplicationContext启动后触发的事件
- ContextStoppedEvent:ApplicationContext停止后触发的事件
- ContextRefreshedEvent:ApplicationContext初始化或刷新完成后触发的事件;(容器初始化完成后调用,所以我们可以利用这个事件做一些初始化操作)
- ContextClosedEvent:ApplicationContext关闭后触发的事件;(如web容器关闭时自动会触发spring容器的关闭,如果是普通java应用,需要调用ctx.registerShutdownHook();注册虚拟机关闭时的钩子才行)

 

event事件

public class TestEvent extends ApplicationEvent {
    private String message;
    public TestEvent(Object source) {
        super(source);
    }

    public void getMessage() {
        System.out.println(message);
    }
    public void setMessage(String message) {
        this.message = message;
    }

}

事件监听者

@Component
public class ApplicationListenerTest implements ApplicationListener<TestEvent> {
    @Override
    public void onApplicationEvent(TestEvent testEvent)
    {
        testEvent.getMessage();
    }
}

事件发布

@RunWith(SpringRunner.class)
@SpringBootTest
public class EventTest {
    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void publishTest() {
        TestEvent testEvent = new TestEvent("");
        testEvent.setMessage("hello world");
        applicationContext.publishEvent(testEvent);
    }
}
输出helloworld

参考:https://blog.csdn.net/pjmike233/article/details/81908540

关于Spring中过滤器,拦截器,监听器的使用及理解:

参考:https://blog.csdn.net/c_royi/article/details/82687724

定时器

普通计时器:通过Timer类对象和TimeTask类来实现

import java.util.Timer;
import java.util.TimerTask;
 
public class TimerDemo {
    public static void main(String[] args) {
        timerTest();
    }    
    public static void timerTest(){
        //创建一个定时器
        Timer timer = new Timer();
        //schedule方法是执行时间定时任务的方法
        timer.schedule(new TimerTask() {
            
            //run方法就是具体需要定时执行的任务
            @Override
            public void run() {
                System.out.println("timer测试!!!");
            }
        }, 1000, 10000);
    }
}

可以借助ApplicationListener作为定时器实现每天00:00执行任务的代码

@Component
public class SystemInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {       
        //创建定时器
        Timer timer = new Timer();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE),0,0,0);
        long timeInterval = 24 * 60 * 60 * 1000;
        timer.schedule(new TimerTask() {
            
            @Override
            public void run() {
                // 每天00:00需要做的事情              
            }
        }, calendar.getTime(), timeInterval);
}
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM