SpringMVC配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
原文地址: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容器注册
例如:
- 如果想使用@Autowired注解,需要在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean。传统的声明方式:<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
- 如果想使用@PersistenceContext注解,需要在Spring容器中声明PersistenceAnnotationBeanPostProcessor Bean。传统的声明:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
- 如果想使用@Required注解,需要在Spring容器中声明RequiredAnnotationBeanPostProcessor Bean。传统声明方式:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
- 如果想使用@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/> 移除了。
- <mvc:annotation-driven />
它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。
SpringMVC配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />的更多相关文章
- 【转载】SpringMVC配置文件详解
转自:https://my.oschina.net/happyBKs/blog/691502 web.xml文件是web应用的部署描述. 在上一节的springMVC示例中 ,idea下的Maven- ...
- SpringMVC配置文件详解
1.<context:annotation-config/> 它的作用是隐式的向Spring容器注册 AutowiredAnnotationBeanPostProcessor, Commo ...
- SpringMVC 配置文件详解
HandlerMapping 处理器映射 HTTP请求被DispatcherServlet拦截后,会调用HandlerMapping来处理,HandlerMapping根据 url<=&g ...
- FastDFS分布式文件系统配置文件详解
一.tracker配置文件详解: # is this config file disabled# false for enabled# true for disableddisabled=false# ...
- Asp.net中web.config配置文件详解(一)
本文摘自Asp.net中web.config配置文件详解 web.config是一个XML文件,用来储存Asp.NET Web应用程序的配置信息,包括数据库连接字符.身份安全验证等,可以出现在Asp. ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- Spring配置文件详解 – applicationContext.xml文件路径
Spring配置文件详解 – applicationContext.xml文件路径 Java编程 spring的配置文件applicationContext.xml的默 ...
- mybatis代码生成器配置文件详解
mybatis代码生成器配置文件详解 更多详见 http://generator.sturgeon.mopaas.com/index.html http://generator.sturgeon.mo ...
随机推荐
- Reactor系列(一)基本概念
基本概念 视频讲解:https://www.bilibili.com/video/av78731069/ 关注公众号,坚持每天3分钟视频学习
- Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- [转帖]PKI系统深入介绍
PKI系统深入介绍 https://blog.csdn.net/liuhuiyi/article/details/7776825 2012年07月23日 20:17:01 liuhuiyi 阅读数 4 ...
- type(),dir(),getattr(),hasattr(), isinstance()用法
1.type(变量) --->输出变量的类型int.float.str or others: 2.dir() ----> dir() 函数不带参数时,返回当前范围内的变量.方法和定义 ...
- asp.net练习①——Application聊天室
已经好几年没写过代码,重新练习起代码,在这做做笔记备忘. aspx页面js代码: <script type="text/javascript"> function sh ...
- 【题解】JSOI2008 最大数
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度.(L>=0) 插 ...
- SQL Join的应用(转)
INNER JOIN LEFT JOIN RIGHT JOIN OUTER JOIN LEFT JOIN EXCLUDING INNER JOIN RIGHT JOIN EXCLUDING INNER ...
- java web浏览器访问工程前面添加一级目录学习笔记
有的时候因为nginx和f5策略问题需要在访问系统的时候前面添加一个前缀以区分不通地方需要用同一个工程的情况: 举个例子:当访问localhost:8080/webDemo/demo/shafei.j ...
- linux 之内存与磁盘
记录工作中常用操作 1. 新建和增加SWAP分区(都必须用root权限,操作过程应该小心谨慎.) 1)新建分区 .以root身份进入控制台(登录系统),输入 swapoff -a #停止所有的swap ...