---
title: "springmvc注解模式需要做的配置"
author: "yjy110"
date: "2015-01-13T15:11:31Z"
tags: []
categories: ["企业架构"]
slug: "springmvc"
draft: false
lastmod: "2015-01-13T15:11:31Z"
id: 2175485
url: "https://yjy110.iteye.com/blog/2175485"
---

&lt;mvc:annotation-driven/&gt;

相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean，配置一些messageconverter。即解决了@Controller注解的使用前提配置。 

 

&lt;context:annotation-config/&gt;

1）隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。

在配置文件中使用&lt;context:annotationconfig/&gt;之前，必须在 &lt;beans&gt; 元素中声明 context 命名空间&lt;context:component-scan/&gt;。

2）是对包进行扫描，实现注释驱动Bean定义，同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。 

 

 

&lt;context:component-scan/&gt; 

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

 

&lt;mvc:annotation-driven /&gt;会自动启动Spring MVC的注解功能，但实际它做了哪些工作呢？

 

&lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;

&lt;property name="order" value="1" /&gt;

&lt;/bean&gt;

 

&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;

&lt;property name="webBindingInitializer"&gt;

  &lt;bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"&gt;

   &lt;property name="conversionService" ref="conversionService" /&gt;

   &lt;property name="validator" ref="validator" /&gt;

  &lt;/bean&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

&lt;bean id="conversionService" class="org.springframework.samples.petclinic.util.PetclinicConversionServiceFactory" /&gt;

&lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt;