2011-08-04 4 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 컨텍스트에 대한봄 초기화 및 파기 방법

내가 가진 :

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

테스트 :

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

auditorium.getBean("Auditorium"); 

를 내가 얻을 :

만 인쇄 "를합니까 조명이 켜짐 " "표시등이 꺼짐"이 인쇄되지 않습니다. 나는 콩을 파괴하기 전에 destroy-method도 호출해야하지만, 나는 무엇을 놓치고 있는지, 또는 얻지 못하고 있는가?

덕분에이 같은

답변

5

그것을 시도 (나는 단지의 경우, 로그에 오류가 없음) : 콩 Spring 컨텍스트에서 사용할 수 있기 때문에, 당신은 방법의 작업을 파괴 관찰 할 수

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

모든 시각. 응용 프로그램 컨텍스트를 닫거나 파기 할 때 내부에서 인스턴스화 된 모든 bean을 파기해야합니다. org.springframework.context.support.AbstractApplicationContext 클래스의 close()destroy() 메소드를 살펴보십시오.