javascript
SpringMVC配置项学习笔记
1.?<mvc:annotation-driven />
<mvc:annotation-driven />是一種簡寫形式,默認會注冊DefaultAnnotationHandlerMapping和DefaultAnnotationHandlerAdapt兩個bean,是spring mvc為@Controller分發請求所必須的,它還提供了@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,讀寫XML的支持(JAXB),讀寫JSON的支持。也可以使用手動配置這兩個bean,不過沒有簡寫的這種方式方便。
2.<context:annotation-config />
作用是向?Spring?容器注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor?以及?RequiredAnnotationBeanPostProcessor?這?4?個BeanPostProcessor。注冊這4個BeanPostProcessor的作用,就是為了你的系統能夠識別相應的注解
例如:
???? (1)、如果你想使用@Autowired注解,那么就必須事先在?Spring?容器中聲明?AutowiredAnnotationBeanPostProcessor的Bean。
???? <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
(2)、如果想使用?@Required的注解,就必須聲明RequiredAnnotationBeanPostProcessor的Bean。
<bean?class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
(3)、如果想使用@ Resource?、@ PostConstruct、@ PreDestroy等注解就必須聲明CommonAnnotationBeanPostProcessor的bean。
<bean?class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>
? (4)、如果想使用@PersistenceContext注解,就必須聲明PersistenceAnnotationBeanPostProcessor的Bean。
?? <bean?class="org.springframework.beans.factory.annotation.PersistenceAnnotationBeanPostProcessor"/>
一般來說,這些注解我們還是比較常用,尤其是Antowired的注解,在自動注入的時候更是經常使用,所以如果總是需要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,于是spring給我們提供<context:annotation-config/>的簡化配置方式,自動幫你完成聲明。
不過,我們使用注解一般都會配置掃描包路徑選項<context:component-scan?base-package=”XX.XX”/>?
? ?? 該配置項其實也包含了自動注入上述processor的功能,因此當使用?<context:component-scan/>?后,就可以將?<context:annotation-config/>?移除了。
?
轉載于:https://www.cnblogs.com/lucky-girl/p/4151130.html
總結
以上是生活随笔為你收集整理的SpringMVC配置项学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ViewPager循环
- 下一篇: 新书《编写可测试的JavaScript代