Spring事件监听实现了观察者模式。本Demo在junit4测试环境中实现
主要有三个类事件类、监听器类、事件发布类(入口)
事件类必须继承 ApplicationEvent,代码如下:
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationEvent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* Created by *** on 2016/3/15.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml")
public class StudentAddEventTest extends ApplicationEvent { private String sname; public StudentAddEventTest(Object source, String sname) {
super(source);
this.sname = sname;
} public String getSname() {
return sname;
}
}
监听器Listener类需实现 ApplicationListener 接口 ApplicationListener可以传入一个泛型的事件类型变量,也可以不传。不传的话,需要在监听到事件发生时(onApplicationEvent)自己判断该事件是不是自己要监听的事件。
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* Created by *** on 2016/3/15.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml")
@Component
public class StudentAddListenerTest implements ApplicationListener<StudentAddEventTest> { @Override
public void onApplicationEvent(StudentAddEventTest studentAddEventTest) {
String sname = studentAddEventTest.getSname();
System.out.println("增加的学生的名字为:::"+sname);
}
}
事件发布类实现了 ApplicationContextAware ,实现这个主要是为了拿到 ApplicationContext实例,进而调用他的 publishEvent方法。感觉不实现ApplicationContextAware应该也可以。。。
:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* Created by *** on 2016/3/15.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml")
@Component
public class StudentAddBeanTest implements ApplicationContextAware {
private static ApplicationContext applicationContext; @Override
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} public void addStudent(String sname){
StudentAddEventTest addEvent = new StudentAddEventTest(this, sname);
applicationContext.publishEvent(addEvent);
} @Test
public void addTest(){
StudentAddBeanTest studentBean = new StudentAddBeanTest();
studentBean.addStudent("张三");
studentBean.addStudent("李四");
} }
需要注意的是 StudentAddListenerTest ——Listener类,StudentAddBeanTest ——事件发布类需在spring容器中注册,Demo中在 applicationContext-indexConfig-test.xml配置了
扫描<context:component-scan> </context:component-scan>路径
然后在类上加了注解@Component,运行测试方法addTest()经过测试,能顺利实现既定目标。