<context:annotation-config> 与<context-component-scan> 的作用

<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。

例子:有三个类   A,B,C,并且B,C的对象被注入到A中.

package com.spring;
public class B {
public B() {
System.out.println("creating bean B: " + this);
}
} package com.spring;
public class C {
public C() {
System.out.println("creating bean C: " + this);
}
} package com.spring2;
import com.spring.B;
import com.spring.C;
public class A {
private B b;
private C c;
public A() {
System.out.println("creating bean A: " + this);
}
public void setBbb(B b) {
System.out.println("setting A.b with " + b);
this.b = b;
}
public void setC(C c) {
System.out.println("setting A.c with " + c);
this.c = c;
}
}

在applicationContext.xml中加入下面的配置 :

<bean id="b"class="com.spring.B"/>
<bean id="c"class="com.spring.C"/>
<bean id="a"class="com.spring2.A">
<property name="b" ref="b"/>
<property name="c" ref="c"/>
</bean>

加载applicationContext.xml配置文件,将得到下面的结果:

creating bean B: com.spring.B@c2ee6
creating bean C: com.spring.C@1e8a1
creating bean A: com.yyy.A@1e1d435
setting A.b with com.spring.B@c2ff5
setting A.c with com.spring.C@1e8a

下面通过注解的方式来简化我们的xml配置文件

首先,我们使用autowire的方式将对象b和c注入到A中:

package com.spring2;
import org.springframework.beans.factory.annotation.Autowired;
import com.spring.B;
import com.spring.C;
public class A {
private B b;
private C c;
public A() {
System.out.println("creating bean A: " + this);
}
@Autowired
public void setB(B b) {
System.out.println("setting A.b with " + b);
this.b = b;
}
@Autowired
public void setC(C c) {
System.out.println("setting A.c with " + c);
this.c = c;
}
}

在applicationContext.xml配置文件中去除属性<property>就简化为下面的样子了

<bean id="b"class="com.spring.B"/>
<bean id="c"class="com.spring.C"/>
<bean id="a"class="com.spring2.A"/>

当我们加载applicationContext.xml配置文件之后,将得到下面的结果:

creating bean B: com.xxx.B@5e5a
creating bean C: com.xxx.C@54a3
creating bean A: com.yyy.A@a3d4

OK, ClassA中显然没有注入属性,结果是错误的的,究竟是因为什么呢?为什么我们的属性没有被注入进去呢?

是因为注解本身并不能够做任何事情,它们只是最基本的组成部分,我们需要能够处理这些注解的处理工具来处理这些注解。

这就是<context:annotation-config> 所做的事情,用于激活那些已经在spring容器里注册过的bean

我们将applicationContext.xml配置文件作如下修改:

<context:annotation-config />
<bean id="b"class="com.spring.B"/>
<bean id="c"class="com.spring.C"/>
<bean id="a"class="com.spring2.A"/>

这回,当我们加载applicationContext.xml配置文件之后,将得到下面的结果:

creating bean B: com.spring.B@178ace
creating bean C: com.spring.C@af0cdld
creating bean A: com.spring2.A@jalfj012
setting A.b with com.spring.B@15663a2
setting A.c with com.spring.C@cd5f8b

和期望的结果一致

但是如果我们将代码作如下修改:

package com.spring;
import org.springframework.stereotype.Component;
@Component
public class B {
public B() {
System.out.println("creating bean B: " + this);
}
} package com.spring;
import org.springframework.stereotype.Component;
@Component
public class C {
public C() {
System.out.println("creating bean C: " + this);
}
} package com.spring2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.spring.B;
import com.spring.C;
@Component
public class A {
private B b;
private C c;
public A() {
System.out.println("creating bean A: " + this);
}
@Autowired
public void setB(B b) {
System.out.println("setting A.b with " + bbb);
this.b = b;
}
@Autowired
public void setC(C c) {
System.out.println("setting A.ccc with " + c);
this.c = c;
}
}

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.spring"/>

当我们加载applicationContext.xml的时候,会得到下面的结果:

creating bean B: com.x.B@1be0f
creating bean C: com.x.C@80d1

这是什么原因呢?

是因为我们仅仅扫描了com.spring包及其子包的类,而class  A是在com.spring2包下,所以就扫描不到了

下面我们在applicationContext.xml中把com.spring2也加入进来:

<context:component-scan base-package="com.spring,com.spring2"/>

然后加载applicationContext.xml就会得到下面的结果:

creating bean B: com.spring.B@cd5f8b
creating bean C: com.spring.C@15ac3c9
creating bean A: com.spring2.A@ec4a87
setting A.b with com.spring.B@cd5f8b
setting A.c with com.spring.C@15ac3c9

回头看下我们的applicationContext.xml文件,已经简化为两行context:component-scan了,是不是很简单?

那如果我们在applicationContext.xml手动加上下面的配置,

也就是说既在applicationContext.xml中手动的注册了A的实例对象,同时,通过component-scan去扫描并注册B,C的对象,如下

<context:component-scan base-package="com.spring"/>
<bean id="a"class="com.spring2.A"/>

结果仍是正确的:

creating bean B: com.spring.B@157aa53
creating bean C: com.spring.C@ec4a87
creating bean A: com.spring2.A@1d64c37
setting A.b with com.spring.B@157aa53
setting A.c with com.spring.C@ec4a87

虽然class A并不是通过扫描的方式注册到容器中的 ,

但是<context:component-scan> 所产生的的处理那些注解的处理器工具,会处理所有绑定到容器上面的bean,不管是通过xml手动注册的还是通过scanning扫描注册的。

那么,如果我们通过下面的方式呢?我们既配置了<context:annotation-config />,又配置了<context:component-scan base-package="com.spring" />,它们都具有处理在容器中注册的bean里面的注解的功能。会不会出现重复注入的情况呢?

<context:annotation-config />
<context:component-scan base-package="com.spring"/>
<bean id="a"class="com.spring2.A"/>

不用担心,不会出现的,结果如下:

creating bean B: com.spring.B@157aa
creating bean C: com.spring.C@ec4a8
creating bean A: com.spring2.A@1d64
setting A.bbb with com.spring.B@157a
setting A.ccc with com.spring.C@ec4a

因为<context:annotation-config />和 <context:component-scan>同时存在的时候,前者会被忽略。

也就是那些@autowire,@resource等注入注解只会被注入一次

哪怕是你手动的注册了多个处理器,spring仍然只会处理一次:

<context:annotation-config />
<context:component-scan base-package="com.spring" />
<bean id="a" class="com.spring2.A" />
<bean id="b1" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="b2" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="b3" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="b4" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

结果仍是正确的:

creating bean B: com.spring.B@157aa53
creating bean C: com.spring.C@ec4a87
creating bean A: com.spring2.A@25d2b2
setting A.b with com.spring.B@157aa53
setting A.c with com.spring.C@ec4a87

Spring <context:annotation-config> 与<context-component-scan> 的作用的更多相关文章

  1. Spring注解详解@Repository、@Component、@Service 和 @Constroller

    概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...

  2. Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别

    <context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config& ...

  3. 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 ...

  4. Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别

    Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...

  5. (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别

    转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...

  6. Spring配置之context:annotation与、component-scan以及annotation-driven

    spring boot帮助我们隐藏了大量的细节,有些配置spring boot都是开启的,因此当我们查看遗留项目使用spring时候遇见问题一定细心排查 <!-- context:annotat ...

  7. spring源码分析之<context:component-scan/>vs<annotation-config/>

    1.<context:annotation-config/> xsd中说明: <xsd:element name="annotation-config"> ...

  8. Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException

    org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...

  9. Spring context:component-scan中使用context:include-filter和context:exclude-filter

    Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...

  10. 【原创】大叔经验分享(16)Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    今天尝试运行一个古老的工程,配置好之后编译通过,结果运行时报错: org.springframework.beans.factory.BeanDefinitionStoreException: Une ...

随机推荐

  1. 【AaronYang第一讲】ASP.NET MVC企业开发的基本环境[资源服务器概念]

    学完了ASP.NET MVC4 IN ACTION 六波以后 企业开发演习 标签:AaronYang  茗洋  EasyUI1.3.4   ASP.NET MVC 3 本篇博客地址:http://ww ...

  2. 阿里云 ssh 登陆请使用(公)ip

    一直以为要要登陆使用的是私有的ip,最后才发现是使用共有ip, 如图 47.52.69.151 > ssh root@47.52.69.151 > 输入密码

  3. 文件批量上传-统一附件管理器-在线预览文件(有互联网和没有两种)--SNF快速开发平台3.0

    实际上在SNF里使用附件管理是非常简单的事情,一句代码就可以搞定.但我也要在这里记录一下统一附件管理器能满足的需求. 通用的附件管理,不要重复开发,调用尽量简洁. 批量文件上传,并对每个文件大小限制, ...

  4. 深入浅出理解c++虚函数

    深入浅出理解c++虚函数   记得几个月前看过C++虚函数的问题,当时其实就看懂了,最近笔试中遇到了虚函数竟然不太确定,所以还是理解的不深刻,所以想通过这篇文章来巩固下. 装逼一刻: 最近,本人思想发 ...

  5. Atitit 快速开发体系建设路线图

    Atitit 快速开发体系建设路线图 1.1. 项目类型划分 哑铃型 橄榄型  直板型(可以立即实行)1 1.2. 解决方案知识库 最佳实践库 最佳流程优化(已成,需要一些整理)2 1.3. 功能模板 ...

  6. (4) MySQL中EXPLAIN执行计划分析

    一. 执行计划能告诉我们什么? SQL如何使用索引 联接查询的执行顺序 查询扫描的数据函数 二. 执行计划中的内容 SQL执行计划的输出可能为多行,每一行代表对一个数据库对象的操作 1. ID列 ID ...

  7. 【网络】TCP和UDP的区别以及TCP的三次握手四次释放

    一.两者区别 1.TCP面向连接的运输层协议,UDP无连接 2.TCP是可靠交付,UDP是尽最大努力交付 3.TCP面向字节流,UDP面向报文 4.TCP是点对点连接的,UDP一对一,一对多,多对多都 ...

  8. 教你一招:Microsoft Office Word已停止工作

    1/按组合键WIN+R打开运行对话框 2/在打开框中键入%USERPROFILE%\AppData\Roaming\Microsoft\Templates,单击“确定”按钮 3/在打开的窗口鼠标右键删 ...

  9. .NET DLL 加密工具

    最近发现了一个软件叫 DotfuscatorPro 混淆加密工具 设置方式如下 1. Settings->Global Options Disable String Encryption 设为  ...

  10. 【Git】简单使用

    [Git & Github] 首先不能混淆两者的概念.git是一个类似于svn的版本管理工具.其可以在本地建立起针对一个项目的众多维度的版本管理体系,提升了开发的效率. 相对的,我们如果想要和 ...