原文地址: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. rocketMq消息的发送和消息消费

    rocketMq消息的发送和消息消费 一.消息推送 public void pushMessage() { String message = "推送消息内容!"; try { De ...

  2. PYTHON 100days学习笔记001:初识python

    现在学习这个确实时间很紧,但是迟早得学,以后PYTHON自动化运维,PYTHON自动测试都需要用的到,甚至可以往数据分析方向发展,刚好最近有数据观组织的python100天计划,就参加了,做好笔记,一 ...

  3. Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式

    配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean. XML形式 <?xml version=&q ...

  4. Thinkphp5 自定义上传文件名

    这几天在做tp5的上传文件模块,项目需求是要把文件名在上传之后修改为 用户名+原文件名的组合形式,在网上找了一会儿发现好像没有类似的文章...只好自己去研究研究了. 之前查看过看云上面的官方手册,文件 ...

  5. Flask_入门

    django是个大而全的框架,flask是一个轻量级的框架. django内部为我们提供了非常多的组件:orm / session / cookie / admin / form / modelfor ...

  6. 【记忆化搜索】Navy maneuvers

    [来源]: 2008年哈尔滨区域赛题目 [题目链接]: http://acm.hdu.edu.cn/showproblem.php?pid=2452 [参考博客]: https://blog.csdn ...

  7. PBOC第八部分和第十一部分关于TYPEA总结(二)——传输协议(ISO14443-4)

    二.传输协议(ISO14443-4)(8,P50 11,P30) 1.选择应答请求(RATS) 使用RATS命令和PICC协商通讯的最大帧长度(FSD和FSC).帧等待时间(FWT)和启动帧保护时间( ...

  8. jenkins+docker+git+etcd实现应用配置文件管理

    两台机器: 一台机器安装gitlab: http://www.cnblogs.com/cjsblogs/p/8716932.html 另一台机器安装etcd+docker+jenkins jenkin ...

  9. [转载]VS2005的工程用VS2010打开后,用VS2005不能打开的解决方法

    首先,在“项目”菜单里,把项目属性“目标平台”改为框架2.0,保存退出. 然后,用记事本或用编辑文本文件的方式打开你的项目文件,后缀为.sln 第一行:把“Microsoft Visual Studi ...

  10. Nginx支持WebSocket服务

    server{ listen ; access_log logs/.jieyun.top.log main; server_name .jieyun.top; #绑定域名 index index.ph ...