Spring 3のbeanカスタムbean特性


Customizing the ネイチャー 保存先 a. beanカスタムbean特性
Springはいくつかのマークインターフェースを提供しています. これらのインターフェースは容器中のbeanの挙動を変えるために使用される.それらはInitializingBenとDispposable Beanを含んでいます.この二つのインターフェースを実現するbeanは、初期化と解析の際に、容器は前者のafterPropertiesset()方法と後者のdestroy()方法を呼び出す.
comple.spring 305.test.customBeabin.po.Impl Cutstom.java
 
 
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class ImplCustom implements InitializingBean,DisposableBean {

	public ImplCustom(){
		System.out.println("this is "+ImplCustom.class+"`s constractor method");
	}
	
	@Override   //InitializingBean
	public void afterPropertiesSet() throws Exception {
		System.out.println("in "+ ImplCustom.class+" afterPropertiesSet() method");
	}

	@Override  //DisposableBean
	public void destroy() throws Exception {
		System.out.println("in "+ ImplCustom.class+" destroy() method");
	}
}
 xmlにbeanを入れる定義
<bean id="implCustom" class="com.spring305.test.customBean.po.ImplCustom"></bean>
 テスト:
@Test
public void test(){
	ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"testCustombean.xml"});
	ImplCustom impl = ctx.getBean("implCustom",ImplCustom.class);
		
}
 同時に、xmlの中でinit-method、destroy-methodを使って初期化のフィードバックと分析のフィードバックを指定することもできます.これでインターフェースを実現する必要がなくなります.
comple.spring 305.test.customBeaビン.po.C.ustomBean.java
public class CustomBean {

	public CustomBean(){
		System.out.println("this is "+CustomBean.class+"`s constractor method");
	}
	
	public void init() {
		System.out.println("in "+ CustomBean.class+" init() method");
	}

	public void destroy()  {
		System.out.println("in "+ CustomBean.class+" destroy() method");
	}
}
 xmlに入れる
<bean id="customBean" class="com.spring305.test.customBean.po.CustomBean" init-method="init" destroy-method="destroy"></bean>
 テスト:
@Test
public void testNoImpliment(){
	AbstractApplicationContext  ctx = new ClassPathXmlApplicationContext(new String[]{"testCustombean.xml"});
	CustomBean impl = ctx.getBean("customBean",CustomBean.class);
	ctx.registerShutdownHook();
}
 最後の文ctx.register Shutdown Hook()は、2.5ドキュメントで定義されているspring IOC容器を閉じる方法です.
上記のように、beansラベルの下で、すべてのbeanタグに初期化のコールバック方法を定義することもできます.default-init-method=「init」