Spring中的ApplicationListener的使用詳解案例(觀察者模式)
1、ApplicationContext
Spring的核心,Context我們通常解釋為上下文環境。ApplicationContext則是應用的容器。 Spring把Bean(object)放在容器中,需要用就通過get方法取出來。在ApplicationContext介面的眾多實現類中,有3個是我們經常用到的(見表1-1),並且使用這3個實現類也基本能滿足我們Java EE應用開發中的絕大部分需求。
表1-1 ApplicationContext介面的常用實現類介紹
類 名 稱 |
功 能 描 述 |
ClassPathXmlApplicationContext |
從類路徑ClassPath中尋找指定的XML配置檔案,找到並裝載完成ApplicationContext的例項化工作。例如: //裝載單個配置檔案例項化ApplicationContext容器 ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml"); //裝載多個配置檔案例項化ApplicationContext容器 String[] configs = {"bean1.xml","bean2.xml","bean3.xml"}; ApplicationContext cxt = new ClassPathXmlApplicationContext(configs); |
FileSystemXmlApplicationContext |
從指定的檔案系統路徑中尋找指定的XML配置檔案,找到並裝載完成ApplicationContext的例項化工作。例如://裝載單個配置檔案例項化ApplicationContext容器 ApplicationContext cxt = new FileSystemXMLApplicationContext("beans.xml"); //裝載多個配置檔案例項化ApplicationContext容器 String[] configs = {"c:/beans1.xml","c:/beans2.xml"}; ApplicationContext cxt = new FileSystemXmlApplicationContext(configs); |
XmlWebApplicationContext |
從Web應用中尋找指定的XML配置檔案,找到並裝載完成ApplicationContext的例項化工作。這是為Web工程量身定製的,使用WebApplicationContextUtils類的getRequiredWebApplicationContext方法可在JSP與Servlet中取得IoC容器的引用 |
2、ApplicationEvent
是個抽象類,裡面只有一個建構函式和一個長整型的timestamp。其原始碼如下:
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}
3、ApplicationListener
是一個介面,裡面只有一個onApplicationEvent方法。如果在上下文中部署一個實現了ApplicationListener介面的bean,那麼每當在一個ApplicationEvent釋出到 ApplicationContext時,呼叫ApplicationContext.publishEvent()方法,這個bean得到通知。類似於Oberver設計模式。
其原始碼如下:
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
好了,這裡簡單介紹下我的使用方式吧。
首先建立一個自己的回撥的類CallBackInfoEvent 去繼承ApplicationEvent:
public class CallBackInfoEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public CallBackInfoEvent(RiskCallBackInput input) {
super(input);
}
}
然後建立ApplicationListener的一個實現類MyListener :
@Component
public class MyListener implements ApplicationListener<CallBackInfoEvent> {
private static final Logger logger = LoggerFactory.getLogger(HuluReportListener.class);
/**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(CallBackInfoEvent event) {
RiskCallBackInput input = (RiskCallBackInput) event.getSource();
if (StringUtils.equals(input.getType(), “1”)) {
//TODO 處理業務邏輯增刪改查
}
}
}
最後在你的業務service中通過呼叫以下程式碼是釋出通知事件:
@Autowired
private ApplicationContext context;
//TODO 業務邏輯
context.publishEvent(new CallBackInfoEvent(Object object));
ok了。