7-1 7-2 본문
생명 주기 와 범위
스프링 컨테이너 생명 주기
스프링 빈 생명 주기
스프링 빈 범위
스프링 컨테이너 생성 - > 스프링 컨테이너 설정 - > 스프링 컨테이너 사용 - > 스프링 컨테이너 종료
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); < 요놈이 컨테이너 생성
ctx.load("classpath:applicationCTX.xml"); <
요놈들이 컨테이너 설정
ctx.refresh(); //별도로 load 메소드를 사용시 refresh를 사용해야 설정이 된다. <
Student studnet = ctx.getBean("student", Student.class); <
.... < 요놈들이 컨테이너 사용
.... <
ctx.close() < 컨테이너 종료
스프링 빈 생명 주기
public class Student implements InitializingBean, DisposableBean
InitializingBean 은 빈이 초기화 과정때 호출이 되며
DisposableBean 는 빈 소멸 과정에서 생성이 된다.
Or @PostConstruct 어노테이션 InitializingBean 을 이용하거나
@PreDestroy 어노테이션은 DisposableBean 을 사용
ex)@PostConstruct
public void init() throws Exception {
System.out.println("afterPropertiesSet()");
}
@PreDestroy
public void destroy1() throws Exception {
System.out.println("destroy()");
}
//////////////////////////////////////
@Override
public void afterPropertiesSet() throws Exception{
System.out.println("afterPropertiesSet()"); <-얘가 초기화 됬을때 호출
}
@Override
public void destroy() throws Exception{ <- 얘가 소멸 됬을때 호출됨.
System.out.println("destroy()");
}
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:applicationCTX.xml");
ctx.refresh(); <- 빈 초기화 ---- 초기화 됬음으로 afterPropertiesSet() 호출 된다.
ctx.close(); <- 빈 소멸(컨테이너 종료) --------- 소멸 됬으니까 destroy() 호출 된다.