0
点赞
收藏
分享

微信扫一扫

SpringMVC中context:annotation-config与mvc:annotation-driven和context:component-scan区别详解

现在常用框架中SpringMVC.xml配置是:

<mvc:annotation-driven/>
<context:component-scan>

那么​​<context:annotation-config/>​​呢?

【1】

激活bean类中要检测的多种注解:

  • 如Spring的@Required和@Autowired;
  • JSR 250’s @PostConstruct, @PreDestroy and @Resource;
  • JAX-WS’s @WebServiceRef;
  • EJB 3’s @EJB;
  • JPA’s@PersistenceContext and @PersistenceUnit

如果你想使用​​@Autowired​​​注解,那么就必须事先在 Spring 容器中声明 ​​AutowiredAnnotationBeanPostProcessor 。​​​而​​AutowiredAnnotationBeanPostProcessor​​​通常是被​​<context:annotation-config/>​​​标签或者​​<context:component-scan/>​​标签注册的。

如果想使用​​@Resource 、@PostConstruct、@PreDestroy​​​等注解就必须声明​​CommonAnnotationBeanPostProcessor。​​​而默认的​​CommonAnnotationBeanPostProcessor​​​通常是被​​<context:annotation-config/>​​​标签或者​​<context:component-scan/>​​​标签注册的。
SpringMVC中context:annotation-config与mvc:annotation-driven和context:component-scan区别详解_表单

如果想使用​​@PersistenceContext​​​注解,就必须声明​​PersistenceAnnotationBeanPostProcessor的Bean。​​JPA中你将会遇到这个注解。

如果想使用 ​​@Required​​​的注解,就必须声明​​RequiredAnnotationBeanPostProcessor的Bean​​​。而默认的​​RequiredAnnotationBeanPostProcessor​​​通常是被​​<context:annotation-config/>​​​标签或者​​<context:component-scan/>​​标签注册的。

​<context:annotation-config/​​​>隐式地向 Spring容器注册这4个​​BeanPostProcessor​​:

AutowiredAnnotationBeanPostProcessor、
RequiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor以及
PersistenceAnnotationBeanPostProcessor

即​​<context:annotation- config/>​​​是用来使上述注解起作用的,也就是说激活已经在application context中注册的bean。之所以这样说是因为​​<context:annotation-config />​​仅能够在已经在已经注册过的bean上面起作用。

对于没有在spring容器中注册的bean,它并不能执行任何操作,也就是说如果你并没有spring容器中注册过bean(spring配置文件中配置bean就是注册(也就是说spring中不存在你想使用的bean)),那么上述的那些注解并不会在你未注册过的bean中起作用。

这里可能会有疑问,比如明明注册了​​AutowiredAnnotationBeanPostProcessor​​​,那么岂不是可以随意使用​​@Autowired​​注解?

这里需要注意的是,@Autowired注解在你使用的时候自动注入的前提是,spring容器中已经有了该bean!那么你可以随意在某个controller或者service使用该注解。并非该注解可以无中生有立即造一个bean给你使用!!

【2】

​<context:component-scan>​​​标签有属性annotation-config默认为true。​​<context:component-scan>做了<context:annotation-config>​​​要做的事情(激活了​​@Required,@Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit)​​​,还额外支持​​@Component,@Repository,@Service,@Controller @RestController,@ControllerAdvice, and @Configuration​​注解。

并且​​<context:component-scan>​​​扫描​​base-package​​并且在application context中注册扫描到的使用注解声明的bean。

这里着重注意第二段话,​​<context:component-scan>​​​该注解可以扫描并注册你使用注解诸如​​@Controller @Service @Component​​​及其他注解声明的bean。也就是说,你不用xml中显示配置,需要的时候尽管用​​@Resource​​​或者​​@Autowired​​来自动注入。

所以配置​​<context:component-scan>​​就不需要配置​​<context:annotation- config/>​

​<context:component-scan>​​​ 下可以拥有若干个 ​​<context:include-filter>​​​ 和 ​​<context:exclude-filter>​​​ 子节点。默认​​fileter(use-default-filters="true")​​​将扫描所有注解组件,若想使用 ​​include-filter​​​等,则将其改为​​false​​​。如下所示一个复杂的​​<context:component-scan/>​​**

<context:component-scan base-package="com.web.annotation" resource-pattern="repository/*.class" 
use-default-filters="true">

<!-- 只包含Repository注解 ,其他不行-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

<!-- 不包含Repository注解 ,其他可以-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>


<!-- 只包含Repository接口及其实现类 ,其他不行-->
<context:include-filter type="assignable" expression="com.web.annotation.repository.UserRepository"/>

<!-- 不包含Repository接口及其实现类 ,其他可以-->
<context:exclude-filter type="assignable" expression="com.web.annotation.repository.UserRepository"/>
</context:component-scan>

【3】

至于该项看前缀就应该知道是springmvc所需要的注解。我们找到对应的实现类是:​​org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser​

通过阅读类注释文档,我们发现这个类主要是用来向工厂中注册了

  • RequestMappingHandlerMapping
  • BeanNameUrlHandlerMapping
  • RequestMappingHandlerAdapter
  • HttpRequestHandlerAdapter
  • SimpleControllerHandlerAdapter
  • ExceptionHandlerExceptionResolver
  • ResponseStatusExceptionResolver
  • DefaultHandlerExceptionResolver

上面几个Bean实例。这几个类都是用来做什么的呢?

前两个是HandlerMapping接口的实现类,用来处理请求映射的。

  • 其中第一个是处理@RequestMapping注解的。
  • 第二个会将controller类的名字映射为请求url(参考BeanNameViewResolver使用详解)。

中间三个是用来处理请求的。具体点说就是确定调用哪个controller的哪个方法来处理当前请求。

  • 第一个处理@Controller注解的处理器,支持自定义方法参数和返回值(很酷)。
  • 第二个是处理继承HttpRequestHandler的处理器。
  • 第三个处理继承自Controller接口的处理器。

后面三个是用来处理异常的解析器,关于异常解析更多内容参考博文SpringMVC中异常处理

另外还将提供以下支持:

① 支持使用​​ConversionService​​​实例对表单参数进行类型转换;
② 支持使用​​​@NumberFormatannotation、@DateTimeFormat​​​注解完成数据类型的格式化;
③ 支持使用​​​@Valid​​​注解对Java bean实例进行JSR 303验证;
④ 支持使用​​​@RequestBody和@ResponseBody​​注解

参考​​<mvc:annotation-driven/>​​标签详解:​SpringMVC中<mvc:annotation-driven/>标签原理与实践详解


举报

相关推荐

0 条评论