正文
<context:annotation-config> 和 <context:component-scan>的区别
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
转自:GOOD spring <context:annotation-config> 跟 <context:component-scan>诠释及区别
<context:annotation-config>
是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解,是一个注解处理工具,也就是只管注入,不管注册bean。
<context:component-scan> 除了具有
<context:annotation-config>的功能之外,
<context:component-scan>
还可以在指定的package下扫描以及注册javabean ,也就是既管注入,又管注册bean。
下面我们通过例子来详细查看他们的区别,
有三个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, 结果正确了。
下面演示<context:annotation-config> 跟 <context:component-scan>区别:
但是如果我们将代码作如下修改:
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,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,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
<context:annotation-config> 和 <context:component-scan>的区别的更多相关文章
-
<input type="button" /> 和<input type="submit" /> 的区别
<input type="button" /> 这就是一个按钮.如果你不写javascript 的话,按下去什么也不会发生.<input type="s ...
-
ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
-
<button>和<input type="button"> 的区别
<button>标签 定义和用法 <button> 标签定义一个按钮. 在 button 元素内部,您可以放置内容,比如文本或图像.这是该元素与使用 input 元素创建的按钮 ...
-
<button>与<input type="button">的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
-
解析button和input type=”button”的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
-
【转】解析<button>和<input type="button"> 的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
-
了解HTML表单之input元素的23种type类型
目录 传统类型 text password file radio checkbox hidden button image reset submit 新增类型 color tel email url ...
-
HTML(七)HTML 表单(form元素介绍,input元素的常用type类型,input元素的常用属性)
前言 表单是网页与用户的交互工具,由一个<form>元素作为容器构成,封装其他任何数量的表单控件,还有其他任何<body>元素里可用的标签 表单能够包含<input> ...
-
HTML表单之input元素的23种type类型
摘自:http://www.cnblogs.com/xiaohuochai/p/5179909.html 了解HTML表单之input元素的23种type类型 随着HTML5的出现,input元素新增 ...
-
HTML中button和input button的区别
button和input button的区别 一句话概括主题:<button>具有<input type="button" ... >相同的作用但是在可操控 ...
随机推荐
-
iOS摇一摇功能、震动功能、简单的摇动动画、生成二维码图片与发送短信等几个功能
有一个开锁的功能,具体的需求就类似于微信的"摇一摇"功能:摇动手机,手机震动,手机上的锁的图片摇动一下,然后发送开锁指令.需求简单,但用到了许多方面的知识. 1.摇一摇 相对这是最 ...
-
hibernate异常:org.hibernate.exception.GenericJDBCException
异常:org.hibernate.exception.GenericJDBCException 提示:Cannot open connection 提示:不能打开链接 一般这个异常是由 java.sq ...
-
servlet之过滤器(转载)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.Filter 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...
-
c++ 定义宏常量
#ifndef __GAME_DATA_H__ #define __GAME_DATA_H__ #ifndef GAME_IS_CREATE_SHOP #define GAME_IS_CREATE_S ...
-
一个突发性的误解C# 引用类型
最近再看IOCP,结果里面的一个赋值过程,造成了误解. test t1 = new test(); test t2 = new test(); test t4 = new test(); t1= t2 ...
-
IE6/IE7浏览器中"float: right"自动换行的解决方法
在项目中,调试IE6/IE7,发现一个bug,那就是:在同一个div包裹着,几个块级元素并列为一行的情况下,设置了float:right的块级元素会自动换行: 代码如下: <p> < ...
-
.Net Core 中使用AutoMapper
1.新建一个类 using AutoMapper; using YourModels; using YourViewModels; namespace YourNamespace { public c ...
-
@+android:id的一些记录
在修改Settings模块的APN列表时,遇到了类似标题中的一些写法. 在网上查了一下: 加上android:表示引用android.R.id里面定义的id资源,如果android.R.id里面确实有 ...
-
setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientific Linux
This is a guide on setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientif ...
-
java的string.split()分割特殊字符时注意点
[1]单个符号作为分隔符 String address="上海|上海市|闵行区|吴中路"; String[] splitAddress=address.s ...
<input type="button" /> 这就是一个按钮.如果你不写javascript 的话,按下去什么也不会发生.<input type="s ...
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
<button>标签 定义和用法 <button> 标签定义一个按钮. 在 button 元素内部,您可以放置内容,比如文本或图像.这是该元素与使用 input 元素创建的按钮 ...
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
目录 传统类型 text password file radio checkbox hidden button image reset submit 新增类型 color tel email url ...
前言 表单是网页与用户的交互工具,由一个<form>元素作为容器构成,封装其他任何数量的表单控件,还有其他任何<body>元素里可用的标签 表单能够包含<input> ...
摘自:http://www.cnblogs.com/xiaohuochai/p/5179909.html 了解HTML表单之input元素的23种type类型 随着HTML5的出现,input元素新增 ...
button和input button的区别 一句话概括主题:<button>具有<input type="button" ... >相同的作用但是在可操控 ...
-
iOS摇一摇功能、震动功能、简单的摇动动画、生成二维码图片与发送短信等几个功能
有一个开锁的功能,具体的需求就类似于微信的"摇一摇"功能:摇动手机,手机震动,手机上的锁的图片摇动一下,然后发送开锁指令.需求简单,但用到了许多方面的知识. 1.摇一摇 相对这是最 ...
-
hibernate异常:org.hibernate.exception.GenericJDBCException
异常:org.hibernate.exception.GenericJDBCException 提示:Cannot open connection 提示:不能打开链接 一般这个异常是由 java.sq ...
-
servlet之过滤器(转载)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.Filter 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...
-
c++ 定义宏常量
#ifndef __GAME_DATA_H__ #define __GAME_DATA_H__ #ifndef GAME_IS_CREATE_SHOP #define GAME_IS_CREATE_S ...
-
一个突发性的误解C# 引用类型
最近再看IOCP,结果里面的一个赋值过程,造成了误解. test t1 = new test(); test t2 = new test(); test t4 = new test(); t1= t2 ...
-
IE6/IE7浏览器中"float: right"自动换行的解决方法
在项目中,调试IE6/IE7,发现一个bug,那就是:在同一个div包裹着,几个块级元素并列为一行的情况下,设置了float:right的块级元素会自动换行: 代码如下: <p> < ...
-
.Net Core 中使用AutoMapper
1.新建一个类 using AutoMapper; using YourModels; using YourViewModels; namespace YourNamespace { public c ...
-
@+android:id的一些记录
在修改Settings模块的APN列表时,遇到了类似标题中的一些写法. 在网上查了一下: 加上android:表示引用android.R.id里面定义的id资源,如果android.R.id里面确实有 ...
-
setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientific Linux
This is a guide on setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientif ...
-
java的string.split()分割特殊字符时注意点
[1]单个符号作为分隔符 String address="上海|上海市|闵行区|吴中路"; String[] splitAddress=address.s ...