在Spring中有这样一种情景就是,我只想监听我想要的那个事件其他的我不需要。OK,在Spring中确实可以实现这种需求,先看看怎样监听的
package com.yohanliyanage.blog.springevents;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class MyEventListener implements ApplicationListener {
private static final Log LOG = LogFactory.getLog(MyEventListener.class);
public void onApplicationEvent(ApplicationEvent event) {
LOG.info("Event Occurred : " + event);
}
}
< ?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Adding the listener class to Spring Container automatically registers it for events -->
<bean class="com.yohanliyanage.blog.springevents.MyEventListener" />