Java, Spring
스프링 빈 생명주기와 스코프, Bean Life Cycle and Scope
ted k
2022. 7. 25. 16:00
빈 생명주기, Bean Life Cycle - 싱글톤 빈 Singleton Bean

- 스프링 컨테이너 생성
- 스프링 빈 생성
- 의존 관계 주입
- 초기화 콜백
- 사용
- 소멸 전 콜백
- 스프링 종료
@Component
public class OrderServiceImpl implements OrderService {
@PostConstruct // annotation으로 객체 생성 후 실행할 함수 등록
public void init() {
System.out.println("초기화 콜백");
}
@PreDestroy // 객체 소멸 전 실행할 함수 등록
public void close() {
System.out.println("소멸 전 콜백");
}
}
빈 생명주기, Bean Life Cycle - Prototype bean 프로토 타입 빈

- 스프링 컨테이너 생성
- 스프링 빈 생성
- 의존 관계 주입
- 초기화 콜백
- 사용
- Garbage Collertor 수거
프로토 타입 빈은 스프링 컨테이너가 빈의 생성까지만 관리를 하여 클라이언트에 빈을 반환하고, 이후 스프링 컨테이너는 생성된 프로토타입 빈을 관리하지 않는다.