转自http://blog.csdn.net/caolaosa...
如今经常使用框架中SpringMVC.xml配置是:<mvc:annotation-driven/>
和<context:component-scan>
那么<context:annotation-config/>
呢?
首先看一下三个注解各自定义:java
<context:annotation-config/>
@Autowired
注解,那么就必须事先在 spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean.@Resource
,@PostConstruct
,@PreDestroy
等注解就必须声明CommonAnnotationBeanPostProcessor@PersistenceContext
注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean.@Required
的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean.使用<context:annotation- config/>
隐式地向 Spring容器注册这4个BeanPostProcessor :web
AutowiredAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor PersistenceAnnotationBeanPostProcessor
即<context:annotation-config/>
是用来使上述注解起做用的,也就是说激活已经在application context中注册的bean.
之因此这样说是由于<context:annotation-config/>
仅可以在spring容器中已经注册过的bean上面起做用.对于没有在spring容器中注册的bean,它并不能执行任何操做,也就是说若是你并无spring容器中注册过bean(spring配置文件中配置bean就是注册),那么上述的那些注解并不会在你未注册过的bean中起做用.spring
<context:component-scan>
<context:component-scan>
作了<context:annotation-config>
要作的事情,还额外支持@Component
,@Repository
,@Service
,@Controller
注解.而且<context:component-scan>
扫描base-package而且在applicationcontext中注册扫描的beans.mvc
因此配置<context:component-scan>
就不须要配置<context:annotation- config/>
app
<mvc:annotation-driven/>
至于该项看前缀就应该知道是springmvc所须要的注解.框架
<mvc:annotation-driven/>
至关于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter.即解决了@Controller
注解的使用前提配置.ui
咱们找到对应的实现类是:url
org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser.
经过阅读类注释文档,咱们发现这个类主要是用来向工厂中注册了.net
RequestMappingHandlerMapping BeanNameUrlHandlerMapping RequestMappingHandlerAdapter HttpRequestHandlerAdapter SimpleControllerHandlerAdapter ExceptionHandlerExceptionResolver ResponseStatusExceptionResolver DefaultHandlerExceptionResolver
上面几个Bean实例.这几个类都是用来作什么的呢?code
前两个是HandlerMapping接口的实现类,用来处理请求映射的.
@RequestMapping
注解的.中间三个是用来处理请求的.具体点说就是肯定调用哪一个controller的哪一个方法来处理当前请求.
理@Controller
注解的处理器,支持自定义方法参数和返回值(很酷).后面三个是用来处理异常的解析器.
另外还将提供如下支持:
① 支持使用ConversionService实例对表单参数进行类型转换;
② 支持使用@NumberFormatannotation,@DateTimeFormat注解完成数据类型的格式化;
③ 支持使用@Valid注解对Java bean实例进行JSR 303验证;
④ 支持使用@RequestBody和@ResponseBody注解
转自:http://blog.csdn.net/sunhuwh/...<annotaion-driven/>
标签:这个标签对应的实现类是org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
仔细阅读它的注释文档能够很明显的看到这个类的做用.解析这个文档:
这个类主要注册8个类的实例:
1.RequestMappingHandlerMapping 2.BeanNameUrlHandlerMapping 3.RequestMappingHandlerAdapter 4.HttpRequestHandlerAdapter 5.SimpleControllerHandlerAdapter 6.ExceptionHandlerExceptionResolver 7.ResponseStatusExceptionResolver 8.DefaultHandlerExceptionResolver
1是处理@RequestMapping注解的,2.将controller类的名字映射为请求url.1和2都实现了HandlerMapping接口,用来处理请求映射.
3是处理@Controller注解的控制器类,4是处理继承HttpRequestHandlerAdapter类的控制器类,5.处理继承SimpleControllerHandlerAdapter类的控制器.因此这三个是用来处理请求的.具体点说就是肯定调用哪一个controller的哪一个方法来处理当前请求.
6,7,8所有继承AbstractHandlerExceptionResolver,这个类实现HandlerExceptionResolver,该接口定义:接口实现的对象能够解决处理器映射,执行期间抛出的异常,还有错误的视图.
因此<annotaion-driven/>
标签主要是用来帮助咱们处理请求映射,决定是哪一个controller的哪一个方法来处理当前请求,异常处理.
<context:component-scan/>
标签:它的实现类是org.springframework.context.annotation.ComponentScanBeanDefinitionParser.
把鼠标放在context:component-scan
上就能够知道有什么做用的,用来扫描该包内被@Repository
,@Service
,@Controller
的注解类,而后注册到工厂中.而且context:component-scan
激活@required
,@resource
,@autowired
,@PostConstruct
,@PreDestroy
,@PersistenceContext
,@PersistenceUnit
.使得在适用该bean的时候用@Autowired
就好了.