스프링 빈이란?
- 스프링 컨테이너가 관리하는 자바 객체
- 스프링이 직접 생성, 관리, 소멸시키는 객체
스프링 빈 생명주기 단계
1. 빈 인스턴스 생성
- 스프링 컨테이너 시작 시 빈 설정 정보 읽음
- 설정 정보 기반으로 빈 객체 생성
- 생성자 호출
@Component
public class MyBean {
public MyBean() {
// 생성자 호출
}
}
2. 의존성 주입
- 빈 생성 후 의존성 주입
- @Autowired, @Resource, 생성자 주입 등으로 수행
@Component
public class MyBean {
private final AnotherBean anotherBean;
@Autowired
public MyBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
}
3. 초기화 콜백
- 의존성 주입 완료 후 초기화 단계 진행
- 빈 사용 준비 완료
3.1 InitializingBean 인터페이스 구현
@Component
public class MyBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// 초기화 로직
}
}
3.2 @PostConstruct 애노테이션 사용 (권장)
@Component
public class MyBean {
@PostConstruct
public void init() {
// 초기화 로직
}
}
3.3 Bean 정의 시 initMethod 속성 사용
@Configuration
public class AppConfig {
@Bean(initMethod = "customInit")
public MyBean myBean() {
return new MyBean();
}
}
public class MyBean {
public void customInit() {
// 초기화 로직
}
}
4. 사용
- 초기화 완료된 빈은 애플리케이션에서 사용
- 스프링 컨테이너 존속 기간 동안 빈 사용 가능
@Service
public class UserService {
private final MyBean myBean;
@Autowired
public UserService(MyBean myBean) {
this.myBean = myBean;
}
public void doSomething() {
// myBean 사용
}
}
5. 소멸 콜백
- 스프링 컨테이너 종료 시 빈 소멸
- 자원 정리 기회 제공
5.1 DisposableBean 인터페이스 구현
@Component
public class MyBean implements DisposableBean {
@Override
public void destroy() throws Exception {
// 정리 로직
}
}
5.2 @PreDestroy 애노테이션 사용 (권장)
@Component
public class MyBean {
@PreDestroy
public void cleanup() {
// 정리 로직
}
}
5.3 Bean 정의 시 destroyMethod 속성 사용
@Configuration
public class AppConfig {
@Bean(destroyMethod = "customDestroy")
public MyBean myBean() {
return new MyBean();
}
}
public class MyBean {
public void customDestroy() {
// 정리 로직
}
}
빈 생명주기 콜백 실행 순서
초기화 콜백 순서
- @PostConstruct 애노테이션 메서드 호출
- InitializingBean 인터페이스의 afterPropertiesSet() 메서드 호출
- @Bean(initMethod = "...") 으로 지정한 메서드 호출
소멸 콜백 순서
- @PreDestroy 애노테이션 메서드 호출
- DisposableBean 인터페이스의 destroy() 메서드 호출
- @Bean(destroyMethod = "...") 으로 지정한 메서드 호출
전체 생명주기 흐름
- 스프링 컨테이너 생성
- 빈 인스턴스 생성 (생성자 호출)
- 의존성 주입
- BeanPostProcessor의 postProcessBeforeInitialization 호출
- @PostConstruct 메서드 호출
- InitializingBean의 afterPropertiesSet() 메서드 호출
- @Bean(initMethod = "...") 으로 지정한 메서드 호출
- BeanPostProcessor의 postProcessAfterInitialization 호출
- 빈 사용
- 스프링 컨테이너 종료 시작
- @PreDestroy 메서드 호출
- DisposableBean의 destroy() 메서드 호출
- @Bean(destroyMethod = "...") 으로 지정한 메서드 호출
- 스프링 컨테이너 종료 완료
정리
기본 생명주기
- 빈 생성 (생성자 호출)
- 의존성 주입
- 초기화 콜백
- 사용
- 소멸 콜백
초기화 방법 (우선순위 순)
- @PostConstruct
- InitializingBean 인터페이스
- @Bean(initMethod="...")
소멸 방법 (우선순위 순)
- @PreDestroy
- DisposableBean 인터페이스
- @Bean(destroyMethod="...")
권장 사항
- @PostConstruct, @PreDestroy 사용 권장
- 외부 라이브러리는 @Bean의 init/destroyMethod 속성 활용
세부 흐름
생성 → 의존성 주입 → BeanPostProcessor(전) → 초기화 콜백 → BeanPostProcessor(후) → 사용 → 소멸 콜백
'Back-End' 카테고리의 다른 글
| [Spring] 어노테이션 세부사항 정리 (1) | 2025.04.24 |
|---|---|
| [Spring] AOP (1) | 2025.04.23 |
| [Spring] 내용 정리 (1) | 2025.04.21 |
| [Spring] 어노테이션 정리 (1) | 2025.04.20 |
| [Spring] 기초 공부1 (1) | 2025.04.19 |