본문 바로가기

카테고리 없음

<context : annotation-config> vs <context : component-scan>

유사하지만 차이점은 존재한다.

둘 다 application-context에 정의해서 사용한다.

<context : annotation-config>

이미 등록된 bean에 대해서만 Annotation을 활성화 시킨다.

어딘가에 bean을 등록하면 @Autowired와 @Qualifier Annotation을 해석해서 가져다 쓰겠다는 의미다.

@Autowired 와 @Qualifier Annotation 이 두가지만 해결할 수 있다.

사용하려면 xml에 bean을 반드시 선언해주어야 한다.

<context : component-scan>

bean등록 여부와 상관없이 사용이 가능하다.

스프링이 알아서 bean을 스캔하고 Annotation을 해석하고 활성화 시킨다.

base-pakage="" 안의 클래스들을 스캔하고 빈 인스턴스를 생성 한 뒤에

아래와 같은 어노테이션이 존재해야지만 빈을 생성한다.

@AutoWired, @Qualifier, @Service, @Component, @Controller, @Repository 등 모든 클래스를 스캔하고

bean에 추가한다.

<context : component-scan>를 사용했다면

<context : annotation-config> 를 사용할 필요가 없다.

다만 이경우엔 base-package를 사용해서 스캔할 패키지의 위치를 지정해두는것이 일반적이다.