javascript
SpringMVC配置项学习笔记
1.?<mvc:annotation-driven />
<mvc:annotation-driven />是一種簡(jiǎn)寫(xiě)形式,默認(rèn)會(huì)注冊(cè)DefaultAnnotationHandlerMapping和DefaultAnnotationHandlerAdapt兩個(gè)bean,是spring mvc為@Controller分發(fā)請(qǐng)求所必須的,它還提供了@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,讀寫(xiě)XML的支持(JAXB),讀寫(xiě)JSON的支持。也可以使用手動(dòng)配置這兩個(gè)bean,不過(guò)沒(méi)有簡(jiǎn)寫(xiě)的這種方式方便。
2.<context:annotation-config />
作用是向?Spring?容器注冊(cè)AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor?以及?RequiredAnnotationBeanPostProcessor?這?4?個(gè)BeanPostProcessor。注冊(cè)這4個(gè)BeanPostProcessor的作用,就是為了你的系統(tǒng)能夠識(shí)別相應(yīng)的注解
例如:
???? (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"/>
一般來(lái)說(shuō),這些注解我們還是比較常用,尤其是Antowired的注解,在自動(dòng)注入的時(shí)候更是經(jīng)常使用,所以如果總是需要按照傳統(tǒng)的方式一條一條配置顯得有些繁瑣和沒(méi)有必要,于是spring給我們提供<context:annotation-config/>的簡(jiǎn)化配置方式,自動(dòng)幫你完成聲明。
不過(guò),我們使用注解一般都會(huì)配置掃描包路徑選項(xiàng)<context:component-scan?base-package=”XX.XX”/>?
? ?? 該配置項(xiàng)其實(shí)也包含了自動(dòng)注入上述processor的功能,因此當(dāng)使用?<context:component-scan/>?后,就可以將?<context:annotation-config/>?移除了。
?
轉(zhuǎn)載于:https://www.cnblogs.com/lucky-girl/p/4151130.html
總結(jié)
以上是生活随笔為你收集整理的SpringMVC配置项学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ViewPager循环
- 下一篇: 新书《编写可测试的JavaScript代