Spring support for @Controller given by vs

后端 未结 3 828
你的背包
你的背包 2020-12-13 22:16

I\'ve been researching what additional capabilities we have when using the mvc:annotation-driven tag and I\'m having a difficult time digesting the results, especially in re

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 23:04

    Both elements serve an entirely different purpose.

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

    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 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 .

    Basically a @Controller without 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 DefaultAnnotationHandlerMapping which processes the @Controller, this is however deprecated).

提交回复
热议问题