前言

Spring MVC 框架提供了几种不同的配置元素来帮助和指示 Spring 容器管理以及注入 bean 。

常用的几个 XML 配置是

  • context:component-scan
  • mvc:annotation-driven
  • context:annotation-config

这些注解的功能相似又有区别,需要认真对待。

context:component-scan

最早的配置,从 Spring 2.5 开始引入的,它是用在 Spring 上的,自然也就可以用在 Spring MVC上。它的引入减少了 Spring 对 XML 配置文件的依赖,不必在 XML 中一个个手工配置 bean 了,也避免了每次更新都要修改 XML 的麻烦。

<context:component-scan base-package="org.controller"/>

在 Spring 应用配置文件中声明如上代码,容器将会扫描 org.controller 目录下的文件,并创建相应的实例。当然要想被创建也需要文件上添加注解,这些注解可以是

  • @Component
  • @Service
  • @Controller
  • @Repository

除了 base-package 之外,还有一个属性是 use-default-filters 默认值是 true,表示使用默认的过滤器,这个的优先级很高,表示会扫描上述四种注解的类并注入。这时如果你在标签内配置了子标签 include-filter 的话就不会起作用,这个表示只扫描配置目录 bean。要想 include-filter 起作用,use-default-filters 就必须配置为 false,如下:

<context:component-scan base-package="com.yifenqi" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

使用 <context:component-scan> 的另外一个好处就是它也能解析 @Autowired 注解和 @Qualifier 注解,因此就没有使用 <context:annotation-config /> 了。

mvc:annotation-driven

添加该配置能够启用 Spring MVC 组件,并做一些默认配置,比如

  • 它会自动注册 HandlerMapping 和 HandlerAdapter,这两个 bean 是 Sring 为 Controller 分发请求所必须的。
  • ConversionService 取代 PropertyEditor 接口
  • 支持 @NumberFormat
  • 支持格式化日期 @DateTimeFormat (Joda)
  • 支持 @Valid,验证 @Controller (JSR-303 Provider)
  • 支持读写 XML (JAXB)
  • 支持读写 JSON(Jackson)

要搞清楚如何做的默认配置,需要阅读文档,我们找到源码中对应的实现类是

org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

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

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

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

  • 第一个是处理 @RequestMapping注解的。
  • 第二个会将 controller 类的名字映射为请求url。

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

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

    后面三个是用来处理异常的解析器。

context:annotation-config

该配置是用来激活那些早就已经在 Spring 容器中存在的 bean 中的 @Autowired 和 @Qualifir 注解。

比如有三个 bean 实例,其中一个把另外两个当做了成员,三个都已经在 Spring 的配置文件中配置了,只是依赖关系没有配置,这种情况下就可以用 context:annotation-config 了。两者缺一不可。

<context:annotation-config />
<bean id="beanA" class="com.example.beans.BeanA"></bean>
<bean id="beanB" class="com.example.beans.BeanB"></bean>
<bean id="beanC" class="com.example.beans.BeanC"></bean>

以上配置等同于如下配置

<bean id="beanA" class="com.example.beans.BeanA">
<property name="beanB" ref="beanB"></property>
<property name="beanC" ref="beanC"></property>
</bean>
<bean id="beanB" class="com.example.beans.BeanB"></bean>
<bean id="beanC" class="com.example.beans.BeanC"></bean>

context:component-scan 能做到同样的事情,并且还会多做一些事情,比如没在容器内注册的也会被它扫描到并注入。可以说 context:annotation-config 是作用域更小更集中的。

结论

简言之,context:component-scan是 Spring为解决 xml 配置文件过于冗长而引入的注解功能,是最全面的一个;而 mvc:annotation-driven是 Spring MVC 要起作用所必须的注解(比如 @RequestMapping等注解),一般项目中都需要两者配合使用。

context:component-scan 和 mvc:annotation-driven的更多相关文章

  1. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  2. [Spring Boot] Use Component Scan to scan for Bean

    Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...

  3. Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

    严重: StandardWrapper.Throwableorg.springframework.beans.factory.BeanCreationException: Error creating ...

  4. SpringMVC配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />

    原文地址:https://www.cnblogs.com/lcngu/p/5080702.html Spring配置文件详解:<context:annotation-config/>和&l ...

  5. Spring 梳理-Spring配置文件 -<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven /> 的区别

    <context:annotation-config/> 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<contex ...

  6. [Spring MVC] - Annotation验证

    使用Spring MVC的Annotation验证可以直接对view model的简单数据验证,注意,这里是简单的,如果model的数据验证需要有一些比较复杂的业务逻辑性在里头,只是使用annotat ...

  7. Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />

    <context:annotation-config/> 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<contex ...

  8. spring mvc 编写处理带参数的Controller

    在上一随笔记录的基础上,现记录编写处理带有参数的Controller. @Controller //这个注解会告知<context:component:scan> 将HomeControl ...

  9. RESTful Demo

    Demo 功能 两个模块, App 与 Admin, App 模块提供增加用户(/add?name=${name})与查询用户(/query/${id}), Admin 模块提供列出所有用户(/lis ...

  10. spring常用的一些注解以及注解注入总结

    常用的spring注解有如下几种: @Controller@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable ...

随机推荐

  1. 洛谷P2504 [HAOI2006]聪明的猴子

    #include<bits/stdc++.h> using namespace std; ; ; int n,m,k,ans; double Max; int monkey[maxn]; ...

  2. @总结 - 10@ Miller-Rabin素性测试与Pollard-Rho因数分解

    目录 @1 - 素性测试:Miller-Rabin算法@ @1.1 - 算法来源@ @1.2 - 算法描述@ @1.3 - 算法实现@ @2 - 因数分解:Pollard-Rho算法@ @2.0 - ...

  3. [***]HZOI20190714 T2熟练剖分

    这题真的神仙,蒟弱表示看题解看不懂……std看了几个小时大概看懂,还有一些细节的东西没有思考. 最难受的是题解和std好像并不是同一个人写的……数组状态不一样……看了好久才看出来f也是前缀和. F[i ...

  4. Strange Way to Express Integers

    I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​ ...

  5. 4 文件操作 支持图片 视频 mp3 文本等

    #文件操作:send_file,支持图片 视频 mp3 文本等@app.route("/img")def img(): return send_file("1.jpg&q ...

  6. 添加SuperSocket的启动代码到 Windows Azure 的 WorkRole 项目

    与其它SuperSocket程序相同,启动代码同样也要写到程序的入口处,如 Windows Azure 的 WorkRole 项目的OnStart() 方法: public override bool ...

  7. [C#] 调试silverlight的时候,总是报“向占位程序传送了空的索引指针”

    这是由于visual studio在调试silverlight的时候,必须和ie一起工作. 按照以下步骤可以把ie设为visual studio的默认浏览器(不用修改操作系统的默认浏览器): 1) 在 ...

  8. svcs (service status) 和 svcadm (service administration) 使用

    1. svcs  显示服务实例的状态信息 svcs - report service status  显示服务状态命令 DESCRIPTION The svcs command displays in ...

  9. tf.variable_scope 参数

    最近在看TensorFlow的变量管理,发现很多代码中tf.variable_scope()参数的数量及意义还不太清楚,特此记录: def __init__(self, name_or_scope, ...

  10. 2018-8-10-win10-uwp-后台获取资源

    title author date CreateTime categories win10 uwp 后台获取资源 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 ...