본문 바로가기

프로그래밍/SPRING 3.X

InitializingBean 인터페이스

InitializingBean 인터페이스

빈 객체의 라이프 사이클과 관련된 인터페이스 중에서 가장 많이 사용되는 것 중의 하나가
  org.springframework.beans.factory.InitializingBean 인터페이다.
 
  객체를 생성하고 프로퍼티를 초기화 하고, 컨테이너관련 설정을 완료한 뒤에 호출되는 메서드를 정의하고 있다.
 
  public interface InitializingBean {
    public void afterPropertiesSet() throws Exception
  }
 
  afterPropertiesSet() 메서드는 주로 빈 객체의 프로퍼티가 모두 올바르게 설정되었는지의 여부를 검사하는 용도
  (프로퍼티 값을 검증하는 코드를 구현)
 
  public abstract class AbstractUrlBasedView extends AbstractView implements InitializingBean {
    ...
    public void afterPropertiesSet() throws Exception {
      if( getUrl() == null ) {
        throw new lllegalArgumentException("Property 'url' is required");
      }
    }
    ...
  }