spring 跟 诠释及区别
<context:annotation-config> 和 <context:component-scan>的区别
Difference between <context:annotation-config> vs <context:component-scan>
<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
sanning的方式)上面的注解。
<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。
下面我们通过例子来详细查看他们的区别,
有三个class A,B,C,并且B,C的对象被注入到A中.
- package com.xxx;
- public class B {
- public B() {
- System.out.println("creating bean B: " + this);
- }
- }
- package com.xxx;
- public class C {
- public C() {
- System.out.println("creating bean C: " + this);
- }
- }
- package com.yyy;
- import com.xxx.B;
- import com.xxx.C;
- public class A {
- private B bbb;
- private C ccc;
- public A() {
- System.out.println("creating bean A: " + this);
- }
- public void setBbb(B bbb) {
- System.out.println("setting A.bbb with " + bbb);
- this.bbb = bbb;
- }
- public void setCcc(C ccc) {
- System.out.println("setting A.ccc with " + ccc);
- this.ccc = ccc;
- }
- }
在applicationContext.xml中加入下面的配置 :
<bean id="bBean"class="com.xxx.B"/>
<bean id="cBean"class="com.xxx.C"/>
<bean id="aBean"class="com.yyy.A">
<property name="bbb" ref="bBean"/>
<property name="ccc" ref="cBean"/>
</bean>
加载applicationContext.xml配置文件,将得到下面的结果:
creating bean B: com.xxx.B@c2ff5
creating bean C: com.xxx.C@1e8a1f6
creating bean A: com.yyy.A@1e152c5
setting A.bbb with com.xxx.B@c2ff5
setting A.ccc with com.xxx.C@1e8a1f6
OK, 这个结果没什么好说的,就是完全通过xml的方式,不过太过时了,下面通过注解的方式来简化我们的xml配置文件
首先,我们使用autowire的方式将对象bbb和ccc注入到A中:
- package com.yyy;
- import org.springframework.beans.factory.annotation.Autowired;
- import com.xxx.B;
- import com.xxx.C;
- public class A {
- private B bbb;
- private C ccc;
- public A() {
- System.out.println("creating bean A: " + this);
- }
- @Autowired
- public void setBbb(B bbb) {
- System.out.println("setting A.bbb with " + bbb);
- this.bbb = bbb;
- }
- @Autowired
- public void setCcc(C ccc) {
- System.out.println("setting A.ccc with " + ccc);
- this.ccc = ccc;
- }
- }
然后,我们就可以从applicationContext.xml中移除下面的配置
<property name="bbb" ref="bBean"/>
<property name="ccc" ref="cBean"/>
移除之后,我们的applicationContext.xml配置文件就简化为下面的样子了
<bean id="bBean"class="com.xxx.B"/>
<bean id="cBean"class="com.xxx.C"/>
<bean id="aBean"class="com.yyy.A"/>
当我们加载applicationContext.xml配置文件之后,将得到下面的结果:
creating bean B: com.xxx.B@5e5a50
creating bean C: com.xxx.C@54a328
creating bean A: com.yyy.A@a3d4cf
OK, 结果是错误的的,究竟是因为什么呢?为什么我们的属性没有被注入进去呢?
是因为注解本身并不能够做任何事情,它们只是最基本的组成部分,我们需要能够处理这些注解的处理工具来处理这些注解
这就是<context:annotation-config> 所做的事情
我们将applicationContext.xml配置文件作如下修改:
<context:annotation-config />
<bean id="bBean"class="com.xxx.B"/>
<bean id="cBean"class="com.xxx.C"/>
<bean id="aBean"class="com.yyy.A"/>
当我们加载applicationContext.xml配置文件之后,将得到下面的结果:
creating bean B: com.xxx.B@15663a2
creating bean C: com.xxx.C@cd5f8b
creating bean A: com.yyy.A@157aa53
setting A.bbb with com.xxx.B@15663a2
setting A.ccc with com.xxx.C@cd5f8b
OK, 结果正确了
但是如果我们将代码作如下修改:
- package com.xxx;
- import org.springframework.stereotype.Component;
- @Component
- public class B {
- public B() {
- System.out.println("creating bean B: " + this);
- }
- }
- package com.xxx;
- import org.springframework.stereotype.Component;
- @Component
- public class C {
- public C() {
- System.out.println("creating bean C: " + this);
- }
- }
- package com.yyy;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import com.xxx.B;
- import com.xxx.C;
- @Component
- public class A {
- private B bbb;
- private C ccc;
- public A() {
- System.out.println("creating bean A: " + this);
- }
- @Autowired
- public void setBbb(B bbb) {
- System.out.println("setting A.bbb with " + bbb);
- this.bbb = bbb;
- }
- @Autowired
- public void setCcc(C ccc) {
- System.out.println("setting A.ccc with " + ccc);
- this.ccc = ccc;
- }
- }
applicationContext.xml配置文件修改为:
<context:annotation-config />
当我们加载applicationContext.xml配置文件之后,却没有任何输出,这是为什么呢?
那是因为<context:annotation-config />仅能够在已经在已经注册过的bean上面起作用。
对于没有在spring容器中注册的bean,它并不能执行任何操作。
但是不用担心,<context:component-scan>除了具有<context:annotation-config
/>的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。
我们将applicationContext.xml配置文件作如下修改:
<context:component-scan base-package="com.xxx"/>
当我们加载applicationContext.xml的时候,会得到下面的结果:
creating bean B: com.xxx.B@1be0f0a
creating bean C: com.xxx.C@80d1ff
这是什么原因呢?
是因为我们仅仅扫描了com.xxx包及其子包的类,而class A是在com.yyy包下,所以就扫描不到了
下面我们在applicationContext.xml中把com.yyy也加入进来:
<context:component-scan base-package="com.xxx"/>
<context:component-scan base-package="com.xxx,com.yyy"/>
然后加载applicationContext.xml就会得到下面的结果:
creating bean B: com.xxx.B@cd5f8b
creating bean C: com.xxx.C@15ac3c9
creating bean A: com.yyy.A@ec4a87
setting A.bbb with com.xxx.B@cd5f8b
setting A.ccc with com.xxx.C@15ac3c9
哇,结果正确啦 !
回头看下我们的applicationContext.xml文件,已经简化为:
<context:component-scan base-package="com.xxx"/>
<context:component-scan base-package="com.xxx,com.yyy"/>
了。
那如果我们在applicationContext.xml手动加上下面的配置,也就是说既在applicationContext.xml中手动的注册了A的实例对象,同时,通过component-scan去扫描并注册B,C的对象
<context:component-scan base-package="com.xxx"/>
<bean id="aBean"class="com.yyy.A"/>
结果仍是正确的:
creating bean B: com.xxx.B@157aa53
creating bean C: com.xxx.C@ec4a87
creating bean A: com.yyy.A@1d64c37
setting A.bbb with com.xxx.B@157aa53
setting A.ccc with com.xxx.C@ec4a87
虽然class A并不是通过扫描的方式注册到容器中的 ,但是<context:component-scan> 所产生的的处理那些注解的处理器工具,会处理所有绑定到容器上面的bean,不管是通过xml手动注册的还是通过scanning扫描注册的。
那么,如果我们通过下面的方式呢?我们既配置了<context:annotation-config />,又配置了<context:component-scan base-package="com.xxx" />,它们都具有处理在容器中注册的bean里面的注解的功能。会不会出现重复注入的情况呢?
<context:annotation-config /><context:component-scan base-package="com.xxx"/><bean id="aBean"class="com.yyy.A"/>
不用担心,不会出现的:
creating bean B: com.xxx.B@157aa53
creating bean C: com.xxx.C@ec4a87
creating bean A: com.yyy.A@1d64c37
setting A.bbb with com.xxx.B@157aa53
setting A.ccc with com.xxx.C@ec4a87
因为<context:annotation-config
/>和<context:component-scan>同时存在的时候,前者会被忽略。也就是那些@autowire,@resource等注入注解只会被注入一次
哪怕是你手动的注册了多个处理器,spring仍然只会处理一次:
- <context:annotation-config />
- <context:component-scan base-package="com.xxx" />
- <bean id="aBean" class="com.yyy.A" />
- <bean id="bla" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
- <bean id="bla1" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
- <bean id="bla2" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
- <bean id="bla3" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
结果仍是正确的:
creating bean B: com.xxx.B@157aa53
creating bean C: com.xxx.C@ec4a87
creating bean A: com.yyy.A@25d2b2
setting A.bbb with com.xxx.B@157aa53
setting A.ccc with com.xxx.C@ec4a87
spring 跟 诠释及区别的更多相关文章
- Spring注解详解@Repository、@Component、@Service 和 @Constroller
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
<context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config& ...
- spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
在运行一个第三方公司交付的项目的时候, 出现: Caused by: java.lang.IllegalStateException: Context namespace element 'annot ...
- Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别
Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...
- (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别
转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...
- Spring配置之context:annotation与、component-scan以及annotation-driven
spring boot帮助我们隐藏了大量的细节,有些配置spring boot都是开启的,因此当我们查看遗留项目使用spring时候遇见问题一定细心排查 <!-- context:annotat ...
- spring源码分析之<context:component-scan/>vs<annotation-config/>
1.<context:annotation-config/> xsd中说明: <xsd:element name="annotation-config"> ...
- Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...
- Spring context:component-scan中使用context:include-filter和context:exclude-filter
Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...
- spring <context:annotation-config> 跟 <context:component-scan>诠释及区别
<context:annotation-config> 和 <context:component-scan>的区别 Difference between <context ...
随机推荐
- python 猜数字游戏(多版本)
原始版本 print('------------------你是sb------------------') temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:" ...
- MyBatis分页实现
目录 分页实现 limit实现分页 RowBounds分页 分页实现 limit实现分页 为什么需要分页? 在学习mybatis等持久层框架的时候,会经常对数据进行增删改查操作,使用最多的是对数据库进 ...
- 关于.NET在中国为什么工资低的分析
引言 近年来,随着软件开发行业的蓬勃发展,越来越多的编程语言和框架进入了市场,成为了不同类型软件开发项目的首选工具.然而,在中国的开发者社区中,.NET 开发人员的工资水平相比其他技术栈,如 Java ...
- PYRAFORMER: 用于长时间序列建模和预测的低复杂度金字塔注意力《PYRAFORMER: LOW-COMPLEXITY PYRAMIDAL ATTENTION FOR LONG-RANGE TIME SERIES MODELING AND FORECASTING》(金字塔注意力模块机制、PAM、CSCM、多尺度)
今天是2022年10月1日,今天重读一遍这篇论文. 10月1日16:48,上次读是4月20日,时间过得好快. 论文:PYRAFORMER: LOW-COMPLEXITY PYRAMIDAL ATTEN ...
- HTML & CSS – dir, direction, writing-mode, ltr (left to rigth), rtl (right to left)
前言 世界上有很多语言的阅读方向是不同的. 英文 中文 (以前才有竖排文字, 现在中文和英语一样了) 阿拉伯文 (Arabic) 面对不同的语言, HTML 和 CSS 就需要不同的写法. 虽然我没有 ...
- EF Core – Unit of Work, DbContext, Transaction 概念解释
前言 踩了一个坑, 下面是 2 个 scope 的调用, 第 1 和 3 是一个 Audit log filter action, 第 2 个是 controller. // open tran // ...
- 【赵渝强老师】HBase的体系架构
一.什么是HBase? HBase是一个基于HDFS之上的分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文"BigTable大表",即:把所 ...
- `->` 操作符重载的注意事项
在 C++ 中,-> 操作符可以被重载,用于对象的指针成员访问. 重载后的 -> 操作符主要用于模拟指针访问行为,常见于智能指针的实现等复杂场景. -> 操作符重载后的调用触发流程: ...
- NTPShell
NTPShell 获取地址:https://github.com/aplyc1a/NTPShell 通过NTP协议来负载C2数据. 编译 gcc ntp.c -lpthread -o ntp 使用 c ...
- Sentinel简单使用(1)
使用场景 在微服务架构中,服务之间会进行大量的调用.为了防止某个服务被过多的请求压垮,导致整个系统崩溃,就需要对流量进行控制.同时,当某个服务出现故障时,为了防止故障扩散到整个系统,需要进行熔断操作. ...
