在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ <mvc:annotation-driven/>背后做了什么

<mvc:annotation-driven/>背后做了什么

在Spring中,我們可以使用注解來減少xml配置文件的配置,如可以使用@Controller,@Autowired,
@RequestMapping,@Service等等;

在需要使用以上注解的時候,我們需要在xml配置文件中寫上:
     <context:component-scan base-package="..."/>
有時候?qū)懮舷旅娴淖⒔饩托校?     <mvc:annotation-driven/>
     
貌似它們的作用有時候是重合的?寫上這兩個注解的時候背后各自都做了什么?網(wǎng)上描述這兩個配置資料的
太亂了           
回答
編輯回答
綰青絲

他們的作用不重合。

context:component-scan 的作用是掃描對應(yīng)的 base-package,將 base-package 及其子包中被 @Component、@Controller、@Service、@Repository 等注解的類,注冊到為 Spring Context,即在上下文環(huán)境中注冊一個這樣的 Bean,這些 Bean 為 Spring MVC 提供了最基礎(chǔ)的支持。


<mvc:annotation-driven/> 是為 MVC 提供額外的支持,參考 Spring 的官方文檔,<mvc:annotation-driven/> 最主要的作用是注冊 HandlerMapping(實現(xiàn)為 DefaultAnnotationHandlerMapping) 和 HandlerAdapter(實現(xiàn)為 AnnotationMethodHandlerAdapter) 兩個類型的 Bean,這兩個 Bean 為 @Controllers(所有控制器) 提供轉(zhuǎn)發(fā)請求的功能。還有一些其他的為 MVC 提供的功能:

  • Using the Spring 3 Type ConversionService as a simpler and more robust alternative to JavaBeans PropertyEditors
  • Support for formatting Number fields with @NumberFormat
  • Support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
  • Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
  • Support for reading and writing XML, if JAXB is on the classpath
  • Support for reading and writing JSON, if Jackson is on the classpath

更多細節(jié)請參考官方文檔。

2017年7月19日 18:22