Spring 梳理-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处理器主要的作用是为了你的系统能够识别相应的注解。 例如:
- 如果想使用@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:annotation-config />仅能够在已经在已经注册过的bean上面起作用。- 对于没有在spring容器中注册的bean,它并不能执行任何操作,也就是说如果你并没有spring容器中注册过bean(spring配置文件中配置bean就是注册(也就是说spring中不存在你想使用的bean)),那么上述的那些注解并不会在你未注册过的bean中起作用。
- 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<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层的配置文件中) - 还额外支持@Component,@Repository,@Service, @Controller @RestController, @ControllerAdvice, and @Configuration 注解。
- 需要添加命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- <context:component-scan/>的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
- <mvc:annotation-driven />
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- 要使用spring mvc中的@Controller注解,就必须要配置<mvc:annotation-driven />,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。
- 需要使用命名空间:
xmlns:mvc="http://www.springframework.org/schema/mvc" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
- 参考 https://www.cnblogs.com/lcngu/p/5080702.html
Spring 梳理-Spring配置文件 -<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven /> 的区别的更多相关文章
- [error] eclipse编写spring等xml配置文件时只有部分提示,tx无提示
eclipse编写spring等xml配置文件时只有<bean>.<context>等有提示,其他标签都没有提示 这时就需要做以下两步操作(下面以事务管理标签为例) 1,添加命 ...
- Spring Boot + Spring Cloud 构建微服务系统(四):容错机制和熔断(Hystrix)
雪崩效应 在微服务架构中,由于服务众多,通常会涉及多个服务层级的调用,而一旦基础服务发生故障,很可能会导致级联故障,进而造成整个系统不可用,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者 ...
- Spring项目的配置文件们(web.xml context servlet springmvc)
我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...
- Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式
Spring EL 一:在Spring xml 配置文件中运用 Spring EL Spring EL 采用 #{Sp Expression Language} 即 #{spring表达式} ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- spring读取prperties配置文件(2)
接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件. 这里我先定义好属性文件"user.propert ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- 玩转Spring Cloud之配置中心(config server &config client)
本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1.2.svn方式 1.3.本地文件方式 1.4.解决配置中包含中文内容返回乱码问题 二.搭建配置消费客户端( ...
随机推荐
- [Python] 将视频转成ASCII符号形式、生成GIF图片
一.简要说明 简述:本文主要展示将视频转成ASCII符号形式展示出来,带音频. 运行环境:Win10/Python3.5. 主要模块: PIL.numpy.shutil. [PIL]: 图像处理 [n ...
- Mysql根据出生日期计算年龄五种方法比较
方法一 SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 AS age 方法一缺陷,就是当日期为未来日期时 ...
- Redis的初识
简介 已经有了Membercache和各种数据库,Redis为什么会产生?Redis纯粹为应用而产生,它是一个高性能的key-value数据库.Redis的出现,很大程序补偿了Memcached这类k ...
- JavaScript算法模式——动态规划和贪心算法
动态规划 动态规划(Dynamic Programming,DP)是一种将复杂问题分解成更小的子问题来解决的优化算法.下面有一些用动态规划来解决实际问题的算法: 最少硬币找零 给定一组硬币的面额,以及 ...
- HTML 全局属性(摘自菜鸟教程)
HTML 全局属性 New : HTML5 新属性. 属性 描述 accesskey 设置访问元素的键盘快捷键. class 规定元素的类名(classname) contenteditableNew ...
- Linux下手动安装JDK
一.删除自带的JDK 查询系统自带JDK rpm -qa | grep java rpm -qa | grep jdk 删除查询出来的文件名 rpm -e --nodeps 文件名 命令执行成功后重新 ...
- NLP(八) 创建自然语言处理管道
一条管道可以被看作一个多阶段的数据流系统,其中一个组件的输出被视为另一个组件的输入 管道特点: 数据始终从一个组件流向另一个组件 组件是一个只考虑输入和输出数据的黑盒 NLP管道应有的功能: 采集输入 ...
- CF1027D Mouse Hunt 思维
Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- SpringDataJpa入门案例及查询详细解析
SpringDataJpaSpring Data JPA 让我们解脱了DA0层的操作,基本上所有CRUD都可以依赖于它来实现,在实际的工作工程中,推荐使用Spring Data JPA+ORM(如:h ...