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コンテキストの場合:

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

テスト:

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

auditorium.getBean("Auditorium");

私は得る:

「ライトがオンになっています」のみを印刷し、「ライトはオフになっています」は印刷しません。Beanを破棄する前に、destroy-methodも呼び出す必要がありますが、何が欠落しているか、取得されていませんか?(念のため、ログにエラーはありません)

ありがとう

4

2 に答える 2

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
于 2011-08-04T16:07:11.350 に答える
0

BeanはSpringコンテキストで常に使用可能であるため、destroyメソッドが機能していることを確認することはできません。アプリケーションコンテキストを閉じる/破棄すると、そのコンテキスト内でインスタンス化されたすべてのBeanが破棄されます。クラスのclose()destroy()メソッドを見てください。org.springframework.context.support.AbstractApplicationContext

于 2011-08-04T16:30:34.700 に答える