// 1. 컨테이너 초기화
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class);
// 2. 컨테이너에서 빈 객체를 구해서 사용
Greeter g = ctx**.getBean**("greeter", Greeter.class);
String msg = g.greet("스프링");
System.out.println(msg);
// 3. 컨테이너 종료
ctx.close();
스프링 컨테이너는 설정 클래스에서 정보를 읽어와 알맞은 빈 객체를 생성하고 각 빈을 연결(의존 주입)하는 작업을 수행
컨테이너를 사용한다는 것은 getBean()과 같은 메서드를 이용해서 컨테이너에 보관된 빈 객체를 구한다는 것
컨테이너 초기화 → 빈 객체의 생성, 의존 주입, 초기화
컨테이너 종료 → 빈 객체의 소멸
두 인터페이스에 빈 객체를 초기화하고 소멸하기 위한 메서드를 정의
org.springframework.beans.factory.InitializingBean
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
org.springframework.beans.factory.DisposableBean
public interface DisposableBean {
void destroy() throws Exception;
}
빈 객체를 생성한 뒤에 초기화 과정이 필요하면, 해당 인터페이스를 상속하고 메서드를 구현하면 됨
ex) ConnectionPool
ex) 채팅 클라이언트