容器上下文事件
2019-12-30 本文已影响0人
充满智慧的白痴
// 定义开始事件
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
// 定义一个context结束时间
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
// 容器注入事件对象
<bean id="cStartEventHandler" class="com.tutorialspoint.CStartEventHandler"/>
<bean id="cStopEventHandler" class="com.tutorialspoint.CStopEventHandler"/>
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
context.start();
context.stop();
context.close();