본문 바로가기

카테고리 없음

[spring] xml 설정 시 annotation-driven / annotation-config / component-scan 차이

728x90

 

<mvc:annotation-driven/>

 

- RequestMapping 을 하는 역할

 

- Spring MVC 컴포넌트들을 디폴트 설정으로 활성화

- Spring MVC @Controller에 요청을 보내기 위한 HandlerMapping과 HandlerAdapter를 Bean 으로 등록.

 

( HandlerMapping: HTTP 요청정보를 통해 컨트롤러를 찾음. ( 요청 URL로 해당 Controller 찾음))

(HandlerAdapter: HandlerMapping 에서 찾은 컨트롤러를 직접 실행)

 

( - Bean 을 생성하기 위해 context:comoponent-scan 을 명시하면

<mvc:annotation-driven/> 태그를 포함하지 않아도 MVC 어플리케이션 작동)

 


<context:annotation-config/>

 

- ApplicationContext 안에 이미 등록된 Bean 들의 Annotation을 활성화 하기 위해 사용된다.

- @Autowired @Qualifier 두 어노테이션만 해결

 

따라서, 위 태그를 사용하더라도 xml에 반드시 bean을 선언해야 한다.

 

 

 


<context:component-scan base-package="패키지명"/>

 

- 설정한 패키지 안의 class를 스캔하고 어노테이션 확인 후 bean 인스턴스 생성

(@Component, @Controller, @Service, @Repository 등의 어노테이션이 존재하면 bean 생성)

(@Autowired, @Qualifier 를 인식하므로 

<context:component-scan base-package="패키지명"/> 를 선언했다면 <context:annotation-config를 선언할 필요 없음)

 

 

 

 


https://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/beans.html#beans-scanning-autodetection

 

6. The IoC container

The BeanFactory provides the underlying basis for Spring’s IoC functionality but it is only used directly in integration with other third-party frameworks and is now largely historical in nature for most users of Spring. The BeanFactory and related inter

docs.spring.io


https://fvor001.tistory.com/79

 

https://www.youtube.com/watch?v=CRjY6BobqGM&list=PLq8wAnVUcTFWhDFFmpJ5fUeoJI53Z83g6&index=28