본문 바로가기

Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Total
Today
Yesterday
관리 메뉴

7-1 7-2 본문

집에서 공부

7-1 7-2

잠이올때마다 2019. 3. 6. 22:56

생명 주기 와 범위

스프링 컨테이너 생명 주기

스프링 빈 생명 주기

스프링 빈 범위


스프링 컨테이너 생성 - > 스프링 컨테이너 설정 - > 스프링 컨테이너 사용 - > 스프링 컨테이너 종료


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() 호출 된다.



'집에서 공부' 카테고리의 다른 글

8-1  (0) 2019.03.06
7-3  (0) 2019.03.06
6-2  (0) 2019.03.06
6 DI설정  (0) 2019.03.06
5-1 5-2  (0) 2019.03.05
Comments