<context:annotation-config>,<context:component-scan>,<mvc:annotation-driven/>区别

<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解,是一个注解处理工具。
<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。

先看一个完全通过XML配置的例子,之后将它改成注解形式。有三个classA,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

接下来使用Autowired的方式将对象bbb和ccc注入到A中:
使用<context:annotation-config />:

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配置文件就简化为下面的样子了。因为注解本身并不能够做任何事情,它们只是最基本的组成部分,我们需要能够处理这些注解的处理工具来处理这些注解,这就是<context:annotation-config> 所做的事情。

<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

如果不加<context:annotation-config />只会得到

creating bean B: com.xxx.B@15663a2
creating bean C: com.xxx.C@cd5f8b
creating bean A: com.yyy.A@157aa53

使用<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: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"/>

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

如果我们同时配置了<context:annotation-config />和<context:component-scan base-package="com.xxx" />,它们都具有处理在容器中注册的bean里面的注解的功能。会不会出现重复注入的情况呢?
在<context:annotation-config />和 <context:component-scan>同时存在的时候,前者会被忽略。也就是那些@autowire,@resource等注入注解只会被注入一次。

使用<mvc:annotation-driven/>:
不同于之前的两个注解来自于Spring,<mvc:annotation-driven/>来自于Spring MVC,对应的实现类是对应的实现类是org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
<mvc:annotation-driven/>标签可简化springmvc的相关配置,默认情况下其会创建并注册如下实例
1.RequestMappingHandlerMapping
2.BeanNameUrlHandlerMapping
3.RequestMappingHandlerAdapter
4.HttpRequestHandlerAdapter
5.SimpleControllerHandlerAdapter 等等。
没有<mvc:annotation-driven />的@Controller只是一个普通的Bean。 它不会绑定到传入的请求,它只是在应用程序上下文中挂起。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容