原文地址:https://www.cnblogs.com/lcngu/p/5080702.html

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

  • <context:annotation-config/>

在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<context:annotation-config/>这样一条配置,它的作用是隐式的向Spring容器注册

                           AutowiredAnnotationBeanPostProcessor,
                           CommonAnnotationBeanPostProcessor,
                           PersistenceAnnotationBeanPostProcessor,
                           RequiredAnnotationBeanPostProcessor 
 这4个BeanPostProcessor.注册这4个bean处理器主要的作用是为了你的系统能够识别相应的注解。                        
 例如:
  1. 如果想使用@Autowired注解,需要在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean。传统的声明方式:<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  2. 如果想使用@PersistenceContext注解,需要在Spring容器中声明PersistenceAnnotationBeanPostProcessor Bean。传统的声明:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  3. 如果想使用@Required注解,需要在Spring容器中声明RequiredAnnotationBeanPostProcessor Bean。传统声明方式:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  4. 如果想使用@Resource、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。传统申明方式: <bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>

所以,如果按照传统声明一条一条去声明注解Bean,就会显得十分繁琐。
因此如果在Spring的配置文件中事先加上<context:annotation-config/>这样一条配置的话,那么所有注解的传统声明就可以被 忽略,即不用在写传统的声明,Spring会自动完成声明。

  • <context:component-scan base-package="com.xx.xx" />

<context:component-scan/>的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

值得注意的是<context:component-scan/>不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和  CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

 
 @Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。注解之后就不需要set/get方法了。
注意:如果有多个配置文件,在最顶层的配置文件(启动类所在的配置文件)中加入<context:component-scan base-package="com.xx.xx" /> 。(如controller层的配置文件中)
  • <mvc:annotation-driven />

它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter

结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。

SpringMVC配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />的更多相关文章

  1. 【转载】SpringMVC配置文件详解

    转自:https://my.oschina.net/happyBKs/blog/691502 web.xml文件是web应用的部署描述. 在上一节的springMVC示例中 ,idea下的Maven- ...

  2. SpringMVC配置文件详解

    1.<context:annotation-config/> 它的作用是隐式的向Spring容器注册 AutowiredAnnotationBeanPostProcessor, Commo ...

  3. SpringMVC 配置文件详解

    HandlerMapping    处理器映射 HTTP请求被DispatcherServlet拦截后,会调用HandlerMapping来处理,HandlerMapping根据 url<=&g ...

  4. FastDFS分布式文件系统配置文件详解

    一.tracker配置文件详解: # is this config file disabled# false for enabled# true for disableddisabled=false# ...

  5. Asp.net中web.config配置文件详解(一)

    本文摘自Asp.net中web.config配置文件详解 web.config是一个XML文件,用来储存Asp.NET Web应用程序的配置信息,包括数据库连接字符.身份安全验证等,可以出现在Asp. ...

  6. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...

  7. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

  8. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  9. mybatis代码生成器配置文件详解

    mybatis代码生成器配置文件详解 更多详见 http://generator.sturgeon.mopaas.com/index.html http://generator.sturgeon.mo ...

随机推荐

  1. babel-plugin-equire - 一个按需加载 echarts 模块的 babel 插件

    参考链接:https://juejin.im/entry/5a1c1bc9f265da430d57bd3f?utm_medium=hao.caibaojian.com&utm_source=h ...

  2. linux-关闭文件

    1.打开参考: http://q.cnblogs.com/q/39275/ http://hi.baidu.com/auxor/item/49b6e929fdf16dc7ed10f197 2.关闭参考 ...

  3. android简易计算器

    activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  4. PYTHON 100days学习笔记007-2:python数据类型补充(2)

    目录 day007:python数据类型补充(2) 1.Python3 元组 1.1 访问元组 1.2 删除元组 1.3 元组运算符 1.4 元组索引,截取 1.5 元组内置函数 2.python3 ...

  5. git常用命令总结——覆盖日常开发全操作

    前言:Git是目前世界上最先进的分布式版本控制系统,对的,最先进! 1. 版本库,又名仓库,repository 可理解成一个目录,目录里的所有文件都可被Git管理,Git可以跟踪每个文件的修改.删除 ...

  6. docker运行报错docker0: iptables: No chain/target/match by that name.

    转自:https://blog.csdn.net/wohaqiyi/article/details/84450562 docker运行报错docker0: iptables: No chain/tar ...

  7. oracle——学习之路(oracle内置函数)

    oracle与很多内置函数,主要分为单行函数与集合函数. 首先要提一下dual表,它oracle的一个表,没有什么实质的东西,不能删除它,否则会造成Oracle无法启动等问题,他有很大用处,可以利用它 ...

  8. PAT B1031 查验身份证(15)

    AC代码 #include <cstdio> #include <iostream> using namespace std; const int max_n = 110; / ...

  9. Go语言Mac、Linux、Windows 下交叉编译

    在很多时候,由于开发的方便,会有这样的场景出现,使用Mac开发或使用Windows开发,需要编译成Linux系统的执行文件,那么如何做到?Go语言提供了非常方便的命令行操作,即可实现. 1.Mac下编 ...

  10. Excel导入异常Cannot get a text value from a numeric cell解决

    POI操作Excel时偶尔会出现Cannot get a text value from a numeric cell的异常错误. 异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类 ...