mvc:annotation-driven 注解

<mvc:annotation-driven/>
相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。

<context:annotation-config/>
1)隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
在配置文件中使用<context:annotationconfig/>之前,必须在 <beans> 元素中声明 context 命名空间<context:component-scan/>。
2)是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。


<context:component-scan/>
配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,除非需要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor两个Processor的功能(例如JPA等)否则就可以将 <context:annotation-config/> 移除了。

(转:http://bbs.shopxx.net/read-htm-tid-10645.html)
spring mvc <mvc:annotation-driven />会自动启动Spring MVC的注解功能,但实际它做了哪些工作呢?

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
  <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
   <property name="conversionService" ref="conversionService" />
   <property name="validator" ref="validator" />
  </bean>
</property>
</bean>
<bean id="conversionService" class="org.springframework.samples.petclinic.util.PetclinicConversionServiceFactory" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

猜你喜欢

转载自perfy315.iteye.com/blog/1447754