13 Spring Bean init - method 와 destroy - method 인 스 턴 스
예시
init - method 와 destroy - method 를 어떻게 사용 하 는 지 보 여 주 는 예 가 있 습 니 다.
실체 류
package com.gp6.initAndDestory;
public class CustomerService {
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void initIt() throws Exception {
System.out.println("Init method after properties are set =====: " + message);
}
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
}
}
설정 파일 은 bean 에서 init - method 와 destroy - method 속성 을 정의 합 니 다.
실행 파일
package com.gp6.initAndDestory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InitAndDestory {
public static void main( String[] args ) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"com/gp6/initAndDestory/InitAndDestory.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
System.out.println(cust);
context.close();
}
}
출력
Init method after properties are set =====: i'm property message
com.gp6.initAndDestory.CustomerService@69267649
Spring Container is destroy! Customer clean up
initit () 방법 이 호출 되 었 습 니 다. 메시지 속성 설정 후 context. close () 호출 후 cleanUp () 방법 을 실행 합 니 다.init - method 와 destroy - methodbean 을 사용 하여 Bena 에서 파일 을 설정 하 는 것 을 권장 합 니 다. InitialingBean 과 Disposable Bean 인 터 페 이 스 를 실행 하 는 것 이 아니 라 불필요 한 결합 코드 를 Spring 에 사용 하 는 것 도 권장 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.