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 ... 
随机推荐
- JS自适应导航栏,菜单栏
			1. 打开 https://github.com/VPenkov/okayNav下载源代码 2.引入两个css样式 <link rel="stylesheet" href=& ... 
- windows下安装Sonar
			1.sonar安装: sonar有三部分组成: 1.服务端:显示分析结果和sonar相关配置 2.客户端:对项目运行源代码进行运算和分析 3.数据库:存储sonar配置和代码分析结果的数据库 2.so ... 
- Second largest node in the BST
			Find the second largest node in the BST 分析: 如果root有右节点,很明显第二大的node有可能在右子树里.唯一不满足的条件就是右子树只有一个node. 这个 ... 
- spring-boot 几个工具类(七)
			环境 jdk 6 tomcat 6.0.53 sts 4.4.2 maven 3.2.5 mysql 5.7 SpringContextHolder SpringContextHolder 可以很方便 ... 
- PAT B1046.猜拳
			课本AC #include <cstdio> int main() { int n, failA = 0, failB = 0; scanf("%d", &n) ... 
- DRF   序列化组件  序列化的两种方式   反序列化   反序列化的校验
			序列化组件 django自带的有序列化组件不过不可控不建议使用(了解) from django.core import serializers class Books(APIView): def ge ... 
- JS基础_嵌套的for循环
			<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ... 
- 查找和杀掉占用GPU显存的进程
			用只有2个G的显卡跑数据就需要在训练之前先把无关进程杀掉,防止跑到一半显存满了 nvidia-smi:显示当前GPU中的线程 kill -9 PID:输入PID以结束线程 
- elementui按需加载
			定义一个ele.js文件 官网:https://element.eleme.io/#/zh-CN/component/quickstart // 方法1 import 'element-ui/lib/ ... 
- java.sql.SQLException: Could not retrieve transaction read-only status from server 问题解决
			网上有2种主要说法 第一种 问题描述: java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not ret ... 
