イベントの伝播

スプリングがどのようにイベント伝播させるか概念図。

実際に試してみる

あらかじめ用意されているイベントに以下がある

  • ContextRefreshedEvent ApplicationContext が初期化またはリフレッシュされたときに発行されます。
  • ContextClosedEvent ApplicationContext がクローズされたときに発行されます。
  • RequestHandledEvent Spring の DispatcherServlet を使用している Web アプリケーションにHTTP リクエストがあった場合に発行されます。

ContextRefreshedEventとContextClosedEventをキャッチしてみる。

Main.java
/**
 * SpringFrameworkTest 2008/07/07
 */
package jp.co.jjjjpppp.springtest.impl;

import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String args) {

		String config = { "src/configerFiles/beans.xml",
				"src/configerFiles/beans2.xml" };
		
		ConfigurableApplicationContext appCon = new FileSystemXmlApplicationContext(config);

		System.out.println(appCon.getMessage("startMessage", null, Locale.getDefault()));
		Man man = (Man) appCon.getBean("man");

		man.buyHouse();
		
		appCon.close();

	}
}
IventCatcher.java
/**
 * SpringFrameworkTest 2008/07/07
 */
package jp.co.jjjjpppp.springtest.impl;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;

public class IventCatcher implements ApplicationListener {

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ContextRefreshedEvent) {
			System.out.println("AppContextがリフレッシュ");
		} else if (event instanceof ContextClosedEvent) {
			System.out.println("AppContextがクローズされた");
		}
	}
}
bean設定ファイルに以下を追記

>|


実行結果
2008/07/28 11:35:42 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1d6096: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1d6096]; startup date [Mon Jul 28 11:35:42 JST 2008]; root of context hierarchy
2008/07/28 11:35:42 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from file [C:\eclipse\workSpace\SpringFrameworkTest\src\configerFiles\beans.xml]
2008/07/28 11:35:42 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from file [C:\eclipse\workSpace\SpringFrameworkTest\src\configerFiles\beans2.xml]
2008/07/28 11:35:42 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
情報: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1d6096]: org.springframework.beans.factory.support.DefaultListableBeanFactory@eb7859
2008/07/28 11:35:42 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
情報: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@eb7859: defining beans [CC1,CC2,oobayashi,man,messageSource,iventCatcher]; root of factory hierarchy
AppContextがリフレッシュ
契約書を作成します。
六本木ヒルズを建築します。
★以下の会社に協力してもらいます。
習志野建設30人2000万
国土交通省4000人20兆円
★以下のステークスホルダーが存在します。
下請け
★以下の工法で建築します
骨組み重視
2008/07/28 11:35:42 org.springframework.context.support.AbstractApplicationContext doClose
情報: Closing org.springframework.context.support.FileSystemXmlApplicationContext@1d6096: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1d6096]; startup date [Mon Jul 28 11:35:42 JST 2008]; root of context hierarchy
AppContextがクローズされた
2008/07/28 11:35:42 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
情報: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@eb7859: defining beans [CC1,CC2,oobayashi,man,messageSource,iventCatcher]; root of factory hierarchy

出力されてOK!!