Spring support for @Controller given by <context:component-scan /> vs <mvc:annotation-driven>

FRAMEWORK/SPRING 2017. 10. 19. 10:52

Both elements serve an entirely different purpose.

<context:component-scan /> is, as the name implies, for component scanning. It by default scans for all beans with the @Component annotation (or "sub"annotations like @Controller@Serviceetc.). It will only register instances of those classes in the application context as beans. That is all.

<mvc:annotation-driven /> is for bootstrapping Spring MVC and it registers, amongst others, a RequestMappingHandlerMapping and RequestMappingHandlerAdapter. The first links requests to a certain method (the @RequestMapping annotation on methods in a @Controller annotated class). The last knows how to execute methods annotated with @RequestMaping.

Now <mvc:annotation-driven /> does nothing for scanning or detecting @Controllers if there are none in the application context then no request mappings are made. Now you have several ways of registering those beans in the application context and one of them is the aforementioned <context:component-scan />.

Basically a @Controller without <mvc:annotation-driven /> is, well, pretty useless as it does nothing but take up memory. It will not be bound to incoming requests, it just hangs around in the application context. It is just another bean like all other beans and nothing special is being done to it. (Recent, but deprecated, versions of Spring register the DefaultAnnotationHandlerMappingwhich processes the @Controller, this is however deprecated).


출처 - https://stackoverflow.com/questions/20551217/spring-support-for-controller-given-by-contextcomponent-scan-vs-mvcannot

'FRAMEWORK > SPRING' 카테고리의 다른 글

Convertor  (0) 2017.10.19
WebMvcConfigurerAdapter  (0) 2017.10.19
Difference between <context:annotation-config> vs <context:component-scan>  (0) 2017.10.19
스프링 시큐리티(SPRING SECURITY)  (0) 2016.10.05
파일 업로드  (0) 2015.01.07
: