spring <context:annotation-config> 跟 <context:component-scan>诠释及区别

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

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.xxx;  
  2. public class B {  
  3.   public B() {  
  4.     System.out.println("creating bean B: " + this);  
  5.   }  
  6. }  
  7.   
  8. package com.xxx;  
  9. public class C {  
  10.   public C() {  
  11.     System.out.println("creating bean C: " + this);  
  12.   }  
  13. }  
  14.   
  15. package com.yyy;  
  16. import com.xxx.B;  
  17. import com.xxx.C;  
  18. public class A {   
  19.   private B bbb;  
  20.   private C ccc;  
  21.   public A() {  
  22.     System.out.println("creating bean A: " + this);  
  23.   }  
  24.   public void setBbb(B bbb) {  
  25.     System.out.println("setting A.bbb with " + bbb);  
  26.     this.bbb = bbb;  
  27.   }  
  28.   public void setCcc(C ccc) {  
  29.     System.out.println("setting A.ccc with " + ccc);  
  30.     this.ccc = ccc;   
  31.   }  
  32. }  

 

 

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx.B"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">
<</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"cBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx.C"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>
</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"aBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.yyy.A"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">
  <</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">property name</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bbb"</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> ref</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bBean"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>
  </span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">property name</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"ccc"</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> ref</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"cBean"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">
</</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">></span></code>

 

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@c2ff5
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@1e8a1f6
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@1e152c5
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bbb with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@c2ff5
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">ccc with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@1e8a1f6</span></code>

 

OK, 这个结果没什么好说的,就是完全通过xml的方式,不过太过时了,下面通过注解的方式来简化我们的xml配置文件

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

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.yyy;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import com.xxx.B;  
  4. import com.xxx.C;  
  5. public class A {   
  6.   private B bbb;  
  7.   private C ccc;  
  8.   public A() {  
  9.     System.out.println("creating bean A: " + this);  
  10.   }  
  11.   @Autowired  
  12.   public void setBbb(B bbb) {  
  13.     System.out.println("setting A.bbb with " + bbb);  
  14.     this.bbb = bbb;  
  15.   }  
  16.   @Autowired  
  17.   public void setCcc(C ccc) {  
  18.     System.out.println("setting A.ccc with " + ccc);  
  19.     this.ccc = ccc;  
  20.   }  
  21. }  
 

 

 

然后,我们就可以从applicationContext.xml中移除下面的配置

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">property name</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bbb"</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> ref</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bBean"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">
<</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">property name</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"ccc"</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> ref</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"cBean"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

移除之后,我们的applicationContext.xml配置文件就简化为下面的样子了

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx.B"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>
</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"cBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx.C"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>
</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"aBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.yyy.A"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@5e5a50
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@54a328
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@a3d4cf</span></code>

 

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

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

这就是<context:annotation-config> 所做的事情

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">annotation</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">config </span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">
<</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"bBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx.B"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>
</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"cBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx.C"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">
<</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"aBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.yyy.A"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@15663a2
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@cd5f8b
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@157aa53
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bbb with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@15663a2
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">ccc with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@cd5f8b</span></code>

 

OK, 结果正确了

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

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.xxx;  
  2. import org.springframework.stereotype.Component;  
  3. @Component  
  4. public class B {  
  5.   public B() {  
  6.     System.out.println("creating bean B: " + this);  
  7.   }  
  8. }  
  9.   
  10. package com.xxx;  
  11. import org.springframework.stereotype.Component;  
  12. @Component  
  13. public class C {  
  14.   public C() {  
  15.     System.out.println("creating bean C: " + this);  
  16.   }  
  17. }  
  18.   
  19. package com.yyy;  
  20. import org.springframework.beans.factory.annotation.Autowired;  
  21. import org.springframework.stereotype.Component;  
  22. import com.xxx.B;  
  23. import com.xxx.C;  
  24. @Component  
  25. public class A {   
  26.   private B bbb;  
  27.   private C ccc;  
  28.   public A() {  
  29.     System.out.println("creating bean A: " + this);  
  30.   }  
  31.   @Autowired  
  32.   public void setBbb(B bbb) {  
  33.     System.out.println("setting A.bbb with " + bbb);  
  34.     this.bbb = bbb;  
  35.   }  
  36.   @Autowired  
  37.   public void setCcc(C ccc) {  
  38.     System.out.println("setting A.ccc with " + ccc);  
  39.     this.ccc = ccc;  
  40.   }  
  41. }  
 

 

 

 

applicationContext.xml配置文件修改为:

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">annotation</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">config </span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

当我们加载applicationContext.xml配置文件之后,却没有任何输出,这是为什么呢?

那是因为<context:annotation-config />仅能够在已经在已经注册过的bean上面起作用。

对于没有在spring容器中注册的bean,它并不能执行任何操作。

但是不用担心,<context:component-scan>除了具有<context:annotation-config />的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">component</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">scan base</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">package</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@1be0f0a
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@80d1ff</span></code>

 

这是什么原因呢?

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

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">component</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">scan base</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">package</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>

</span></code><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;"><</span><span class="pln" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">context</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">:</span><span class="pln" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">component</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">-</span><span class="pln" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">scan base</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">-</span><span class="kwd" style="font-family: 'Courier New', Courier, monospace; color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">package</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">=</span><span class="str" style="font-family: 'Courier New', Courier, monospace; color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">"com.xxx,com.yyy"</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">/></span>
然后加载applicationContext.xml就会得到下面的结果:<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"></code>
<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@cd5f8b
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@15ac3c9
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@ec4a87
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bbb with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@cd5f8b
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">ccc with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@15ac3c9</span></code>

 

哇,结果正确啦 !

回头看下我们的applicationContext.xml文件,已经简化为:

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">component</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">scan base</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">package</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>

</span></code><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;"><</span><span class="pln" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">context</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">:</span><span class="pln" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">component</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">-</span><span class="pln" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">scan base</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">-</span><span class="kwd" style="font-family: 'Courier New', Courier, monospace; color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">package</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">=</span><span class="str" style="font-family: 'Courier New', Courier, monospace; color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">"com.xxx,com.yyy"</span><span class="pun" style="font-family: 'Courier New', Courier, monospace; border: 0px; padding: 0px; background-color: transparent; margin: 0px; font-size: 1em; vertical-align: baseline;">/></span>

 

了。

 

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">component</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">scan base</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">package</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/>
</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"aBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.yyy.A"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

结果仍是正确的:

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@157aa53
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@ec4a87
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@1d64c37
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bbb with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@157aa53
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">ccc with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@ec4a87</span></code>

 

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

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

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">annotation</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">config </span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">context</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">component</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">scan base</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">-</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">package</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.xxx"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"><</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bean id</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"aBean"</span><span class="kwd" style="color: rgb(0, 0, 139); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">class</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">=</span><span class="str" style="color: rgb(128, 0, 0); border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">"com.yyy.A"</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">/></span></code>

 

不用担心,不会出现的:

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@157aa53
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@ec4a87
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@1d64c37
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bbb with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@157aa53
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">ccc with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@ec4a87</span></code>

 

因为<context:annotation-config />和 <context:component-scan>同时存在的时候,前者会被忽略。也就是那些@autowire,@resource等注入注解只会被注入一次

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

 

[xml]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <context:annotation-config />  
  2. <context:component-scan base-package="com.xxx" />  
  3. <bean id="aBean" class="com.yyy.A" />  
  4. <bean id="bla" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  
  5. <bean id="bla1" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  
  6. <bean id="bla2" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  
  7. <bean id="bla3" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  

 

 

结果仍是正确的:

 

<code style="border: 0px; padding: 0px; margin: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline;"><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">creating bean B</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@157aa53
creating bean C</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@ec4a87
creating bean A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;"> com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">yyy</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">A@25d2b2
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">bbb with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">B@157aa53
setting A</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">ccc with com</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">xxx</span><span class="pun" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; padding: 0px; background-color: transparent; margin: 0px; vertical-align: baseline;">C@ec4a87</span></code>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值