Spring事件監聽Demo


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()經過測試,能順利實現既定目標。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM