Spring - Bean lifecycle

salgu·2022년 4월 18일
0

Spring

목록 보기
5/21

컨테이너가 관리하는 Bean의 라이프사이클은

객체생성 - 의존설정 - 초기화 - 소멸

스프링 컨테이너가 초기화 할 때, 빈 객체를 설정 정보에 따라 생성하고, 의존 관계를 설정합니다.
의존 설정이 완료되면, 빈 객체가 지정한 메소드를 호출해 초기화합니다.
컨테이너가 종료될 시 빈 객체가 지정한 메소드를 호출해 빈 객체의 소멸을 처리합니다.

// 1. 컨테이너 초기화
ApplicationContext context = new AnnotationConfigApplicationContext(DaoFactory.class);
// 2. 컨테이너 사용
UserDao dao = context.getBean("userDao", UserDao.class);
// 3. 컨테이너 종료
context.close();      
  • 빈 객체는 스프링의 InitializingBean, DisposableBean 인터페이스를 통해 초기화, 소멸 메소드를 구현할 수 있다.
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}
public interface DisposableBean{
    void destroy() throws Exception;
}
  • 자바 메소드 구현, @Bean 어노테이션에 명시를 통한 방법
@Bean(initMethod="connectDB", destroyMethod="commit")






reference : https://velog.io/@fortice/Spring-%EB%B9%88-%EB%9D%BC%EC%9D%B4%ED%94%84%EC%82%AC%EC%9D%B4%ED%81%B4-%EC%8A%A4%EC%BD%94%ED%94%84, https://devlog-wjdrbs96.tistory.com/321

profile
https://github.com/leeeesanggyu, leeeesanggyu@gmail.com

0개의 댓글