2011-08-04 7 views
2
package com.idol; 

public class Auditorium {  
Auditorium(){ 
} 
public void turnOnLights() { 
    System.out.println("Lights are turned on"); 
} 
public void turnOffLights(){ 
    System.out.println("Lights are turned off"); 
} 

} XMLコンテキストの場合春のinitおよびdestroyメソッド

私が持っている:

<bean id="Auditorium" class="com.idol.Auditorium" init-method="turnOnLights" destroy-method="turnOffLights"/> 

テスト:

ApplicationContext auditorium = 
     new ClassPathXmlApplicationContext("ApplicationContextVer6.xml"); 

auditorium.getBean("Auditorium"); 

を私が手:

のみ印刷」をいライトが点灯している " 「ライトは消灯しています」と表示されません。私は豆を破壊する前に破壊方法も呼び出すべきですが、私は何が欠けているのか、それともないのでしょうか? (私は念のため、ログにエラーがない)

おかげ

答えて

5

はこのようにそれを試してみてください。

final ConfigurableApplicationContext auditorium = 
     new ClassPathXmlApplicationContext("ApplicationContextVer6.xml"); 
auditorium.getBean("Auditorium"); 
auditorium.close(); // thx Nathan 

// auditorium.refresh() will also turn the lights off 
// before turning them on again 
+0

ライトがまだ点灯していません:-) – Aubergine

+0

私は理解しましたが、破壊方法はまだ呼び出されていません。 – Aubergine

+0

@Aubergineあなたは正しいです。しかし、このバージョンは動作します。 –

0

豆は、すべてのSpringコンテキストで利用可能なので、あなたは、メソッドの作業を破壊観察することができません時間。アプリケーション・コンテキストを閉じる/破棄すると、その内部でインスタンス化されたすべてのBeanを破棄する必要があります。 org.springframework.context.support.AbstractApplicationContextクラスのclose()destroy()メソッドを見てください。

関連する問題