Spring事件監聽實現了觀察者模式。本Demo在junit4測試環境中實現spring
主要有三個類事件類、監聽器類、事件發佈類(入口)app
事件類必須繼承 ApplicationEvent,代碼以下:ide
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應該也能夠。。。this
:spa
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配置了code
掃描<context:component-scan> </context:component-scan>路徑component
而後在類上加了註解@Component,運行測試方法addTest()通過測試,能順利實現既定目標。xml