Spring 생명주기(컨테이너/Bean)✍

김민석·2021년 1월 25일
0

Spring 컨테이너 생명주기
(Life Cycle) ✍

Spring의 컨테이너와 빈의 생명주기(Life Cycle)에 대한 정리.

1.컨테이너의 생명주기

컨테이너의 생명주기는 생성->빈 설정->사용->소멸 의 순서를 갖는다.

GenericXmlApplicationContext ctx = 
		new GenericXmlApplicationContext("classpath:appCtx.xml"); 
        //생성                
BookRegisterService bookRegisterService=
		ctx.getBean("bookRegisterService",BookRegisterService.class);
        //빈 설정                
for(int i=0;i<bNums.length;i++) {
		Book book = new Book(bNums[i], bTitles[i], true, null);
		bookRegisterService.register(book);
	}
    	//사용        
ctx.close();
	//소멸

※ 스프링 컨테이너 초기화 / 종료 시 수행되는 작업

▶ 컨테이너 초기화 : 빈 객체 생성 및 주입

▶ 컨테이너 종료 : 빈 객체 소멸

2.빈(Bean)의 생명주기

빈은 객체생성 의존설정 초기화 소멸의 선순서를 갖는다.
InitializingBean와 DisposableBean의 인터페이스를 사용하면 언제 생성되고 언제 소멸되는지 확인 할 수 있다.

public class BookDao implements InitializingBean, DisposableBean {	
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("빈(Bean)객체 생성 단계");
	}
	@Override
	public void destroy() throws Exception {
		System.out.println("빈(Bean)객체 소멸 단계");
	}	
}
profile
언제나 왜 Why?

0개의 댓글