原理:ApplicationContextAware接口提供了publishEvent方法,實現了Observe(觀察者)設計模式的傳播機制,實現了對bean的傳播。經過ApplicationContextAware咱們能夠把系統中全部ApplicationEvent傳播給系統中全部的ApplicationListener。spring
直接上代碼:設計模式
1.定義本身的監聽事件app
2.定義本身的監聽器(負責處理本身的監聽事件)函數
3.定義一個bean觸發監聽事件測試
4.測試this
package com.test.eventListener; import org.springframework.context.ApplicationEvent; /** * @author admin * @date 2018/5/17 17:37 * 新建StudentAddEvent類,實現抽象類org.springframework.context.ApplicationEvent * StudentAddEvent類中須要實現本身的構造函數 * 增長學生監聽事件 */ public class StudentAddEvent extends ApplicationEvent { private static final long serialVersionUID = 20L; /** * 學生姓名 */ private String name; /** * @param source */ public StudentAddEvent(Object source, String name) { super(source); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.test.eventListener; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * @author admin * @date 2018/5/17 17:41 * 新建StudentAddListener類,實現接口org.springframework.context.ApplicationListener中的onApplicationEvent方法, * 在該方法中只處理StudentAddEvent類型的ApplicationEvent事件 * 定義StudentAddListener監聽器 */ @Component public class StudentAddListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { // 1.判斷是不是增長學生對象的事件 if (!(event instanceof StudentAddEvent)) { return; } // 2.是增長學生事件的對象,進行邏輯處理,好比記日誌、積分等 StudentAddEvent studentAddEvent = (StudentAddEvent) event; System.out.println("增長了學生:" + studentAddEvent.getName()); } }
package com.test.eventListener; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * @author admin * @date 2018/5/17 17:45 * 定義StudentAddBean觸發StudentAddEvent事件 * 新建StudentAddBean類,實現接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法, * 在構造bean的時候注入Spring的上下文對象,以便經過Spring上下文對象的publishEvent方法來觸發StudentAddEvent事件 */ @Component public class StudentAddBean implements ApplicationContextAware { /** * 定義Spring上下文對象 */ private ApplicationContext applicationContext = null; /* * (non-Javadoc) * * @see * org.springframework.context.ApplicationContextAware#setApplicationContext * (org.springframework.context.ApplicationContext) */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 增長一個學生 * * @param studentName */ public void addStudent(String studentName) { // 1.構造一個增長學生的事件 StudentAddEvent aStudentEvent = new StudentAddEvent( applicationContext, studentName); // 2.觸發增長學生事件 applicationContext.publishEvent(aStudentEvent); } }
package com.test.eventListener; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author admin * @date 2018/5/17 17:55 * ApplicationContext在運行期會自動檢測到全部實現了ApplicationListener的bean對象,並將其做爲事件接收對象。 * 當ApplicationContext的publishEvent方法被觸發時,每一個實現了ApplicationListener接口的bean都會收到ApplicationEvent對象, * 每一個ApplicationListener可根據事件類型只接收處理本身感興趣的事件,好比上面的StudentAddListener只接收StudentAddEvent事件。 */ public class EventListenerTest { public static void main(String[] args) { String[] xmlConfig = new String[] { "spring/spring.xml" }; // 使用ApplicationContext來初始化系統 ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig); StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean"); studentBean.addStudent("張三"); studentBean.addStudent("李四"); } }