![](/img/trans.png)
[英]Difference between <context:annotation-config> and <context:component-scan>
[英]What's the difference between <mvc:annotation-driven /> and <context:annotation-config /> in servlet?
我正在從Spring 2.5遷移到Spring 3。
他們引入了<mvc:annotation-driven />
,它做了一些黑魔法。 這應該只在servlet配置文件中聲明。
在Spring 2.5中,我剛剛使用<context:annotation-config />
和<context:component-scan base='...'/>
標簽在application-context.xml
和dispatcher servlet配置XML中聲明,並使用適當的基礎包掃描。
所以我想知道mvc:annotation-driven
和context:annotation-config
servlet配置中的context:annotation-config
標簽之間的區別是什么?我可以在Spring 3配置文件中消除什么?
<context:annotation-config>
聲明支持一般注釋,例如@Required
,@ @Autowired
,@ @PostConstruct
等。
<mvc:annotation-driven />
明顯式支持注釋驅動的MVC控制器(即@Controller
@RequestMapping
, @Controller
Controller,雖然支持這些是默認行為),以及通過@Valid
和消息體編組添加對聲明性驗證的支持使用@RequestBody
/ ResponseBody
。
關於在Spring文檔中使用<mvc:annotation-driven />
更多細節。 簡而言之, <mvc:annotation-driven />
使您可以更好地控制Spring MVC的內部工作方式。 除非您需要上述文檔部分中概述的一個或多個功能,否則無需使用它。
此外,還有其他“注釋驅動”標簽可用於在其他Spring模塊中提供其他功能。 例如, <transaction:annotation-driven />
允許使用@Transaction注釋, <task:annotation-driven />
是@Scheduled et al ...
mvc:annotation-driven是Spring 3.0中添加的標記,它執行以下操作:
context:annotation-config在定義的相同應用程序上下文中查找bean上的注釋,並聲明支持所有常規注釋,如@ Autowired,@ Resource,@ Required,@ PostConstruct等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.