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

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

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

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

【1】 <context:annotation-config/>

激活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/>标签注册的。
在这里插入图片描述

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

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

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

AutowiredAnnotationBeanPostProcessorRequiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor以及
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>

<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】 <mvc:annotation-driven/>

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

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

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

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

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

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

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

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

另外还将提供以下支持:

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

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

  • 18
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值