// 定义开始事件
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();
自定义事件
网友评论