annotation-config 和 servletcomponentscan-scan 的区别

Spring 开启Annotation &context:annotation-config&和 &context:component-scan&诠释及区别 - CSDN博客
Spring 开启Annotation &context:annotation-config&和 &context:component-scan&诠释及区别
Spring 开启Annotation &context:annotation-config&和 &context:component-scan&诠释及区别
&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 。
下面我们通过例子来详细查看他们的区别,有三个class
A,B,C,并且B,C的对象被注入到A中.
package com.
public class B {
public B() {
System.out.println("creating bean B: " + this);
package com.
public class C {
public C() {
System.out.println("creating bean C: " + this);
package com.
import com.xxx.B;
import com.xxx.C;
public class A {
public A() {
System.out.println("creating bean A: " + this);
public void setBbb(B bbb) {
System.out.println("setting A.bbb with " + bbb);
this.bbb =
public void setCcc(C ccc) {
System.out.println("setting A.ccc with " + ccc);
this.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"/&
加载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配置文件
下边看看怎么使用&context:annotation-config&和 &context:component-scan&
首先,我们使用autowire的方式将对象bbb和ccc注入到A中:
package com.
import org.springframework.beans.factory.annotation.A
import com.xxx.B;
import com.xxx.C;
public class A {
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 =
@Autowired
public void setCcc(C ccc) {
System.out.println("setting A.ccc with " + ccc);
this.ccc =
然后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, ClassA中显然没有注入属性,结果是错误的的,究竟是因为什么呢?为什么我们的属性没有被注入进去呢?
是因为注解本身并不能够做任何事情,它们只是最基本的组成部分,我们需要能够处理这些注解的处理工具来处理这些注解。
这就是&context:annotation-config& 所做的事情,用于激活那些已经在spring容器里注册过的bean
我们将applicationContext.xml配置文件作如下修改:
&context:annotation-config /&
&bean id="bBean"class="com.xxx.B"/&
&bean id="cBean"class="com.xxx.C"/&
&bean id="aBean"class="com.yyy.A"/&
这回,当我们加载applicationContext.xml配置文件之后,将得到下面的结果:
creating bean B: com.xxx.B@15663a2
creating bean C: com.xxx.C@cd5f8b
creating bean A: com.yyy.A@157aa53
setting A.bbb with com.xxx.B@15663a2
setting A.ccc with com.xxx.C@cd5f8b
OK, 结果正确了
但是如果我们将代码作如下修改:
package com.
import org.
@Component
public class B {
public B() {
System.out.println("creating bean B: " + this);
package com.
import org.
@Component
public class C {
public C() {
System.out.println("creating bean C: " + this);
package com.
import org.springframework.beans.factory.annotation.A
import org.
import com.xxx.B;
import com.xxx.C;
@Component
public class A {
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 =
@Autowired
public void setCcc(C ccc) {
System.out.println("setting A.ccc with " + ccc);
this.ccc =
applicationContext.xml配置文件修改为:
当我们加载applicationContext.xml配置文件之后,却没有任何输出,这是为什么呢?
那是因为&context:annotation-config /&仅能够在已经在已经注册过的bean上面起作用。
对于没有在spring容器中注册的bean,它并不能执行任何操作。
但是不用担心,&context:component-scan&除了具有&context:annotation-config /&的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。
我们将applicationContext.xml配置文件作如下修改:
&context:component-scan base-package="com.xxx"/&
当我们加载applicationContext.xml的时候,会得到下面的结果:
creating bean B: com.xxx.B@1be0f0a
creating bean C: com.xxx.C@80d1ff
这是什么原因呢?
是因为我们仅仅扫描了com.xxx包及其子包的类,而class
A是在com.yyy包下,所以就扫描不到了
下面我们在applicationContext.xml中把com.yyy也加入进来:
&context:component-scan base-package="com.xxx"/&
&context:component-scan base-package="com.xxx,com.yyy"/&
然后加载applicationContext.xml就会得到下面的结果:
creating bean B: com.xxx.B@cd5f8b
creating bean C: com.xxx.C@15ac3c9
creating bean A: com.yyy.A@ec4a87
setting A.bbb with com.xxx.B@cd5f8b
setting A.ccc with com.xxx.C@15ac3c9
哇,结果正确啦 !
回头看下我们的applicationContext.xml文件,已经简化为两行context:component-scan了,是不是很简单?
那如果我们在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
本文已收录于以下专栏:
相关文章推荐
Difference between
 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
sanning的方式)上...
Difference between
 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
Difference between
 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
Spring 开启Annotation
和 诠释及区别
 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
sanning的方式)上面的注解。
除了具有的功能之外,还可以在指定...
最近在看项目组写好的代码,遇到了这个疑惑,在博客园上看到一篇很好的解释帖子,特转载过来方便以后查看。
Spring 开启Annotation
和 诠释及区别
是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sannin...
是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。
除了具有的功能之外,还可以在指定的package下扫描以及注...
在这篇文章中,我将介绍和标签的区别,将来再使用它们的时候能够清楚的知道你在干什么
首先,是用来激活已经在application context中注册的bean。注意,不管你使用那种注册的方式都是可以...
他的最新文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)<context:annotation-config>与<context:component-scan>的区别 - MikeSun - CSDN博客
<context:annotation-config>与<context:component-scan>的区别
#FRAMEWORK#
<context:annotation-config>与<context:component-scan>的区别
声明:本文转自讲解十分详细,十分精彩,特此分享到CSDN;有时间的话,我会翻译一下,BUT,NOT TODAY;
<context:annotation-config> is used to activate annotations in beans already registered in the application context (no matter if they were defined with XML or by package scanning).
<context:component-scan> can also do what <context:annotation-config> does but <context:component-scan> also scans packages to find and register beans within the application context.
I’ll use some examples to show the differences/similarities.
Lets start with a basic setup of three beans of type A, B and C, with B and C being injected into A.
package com.
public class B {
public B() {
System.out.println("creating bean B: " + this);
package com.
public class C {
public C() {
System.out.println("creating bean C: " + this);
package com.
import com.xxx.B;
import com.xxx.C;
public class A {
public A() {
System.out.println("creating bean A: " + this);
public void setBbb(B bbb) {
System.out.println("setting A.bbb with " + bbb);
this.bbb =
public void setCcc(C ccc) {
System.out.println("setting A.ccc with " + ccc);
this.ccc =
With the following XML configuration :
<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" />
Loading the context produces the following output:
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, this is the expected output. But this is “old style” Spring. Now we have annotations so lets use those to simplify the XML.
First, lets autowire the bbb and ccc properties on bean A like so:
package com.
import org.springframework.beans.factory.annotation.A
import com.xxx.B;
import com.xxx.C;
public class A {
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 =
@Autowired
public void setCcc(C ccc) {
System.out.println("setting A.ccc with " + ccc);
this.ccc =
This allows me to remove the following rows from the XML:
<property name="bbb" ref="bBean" />
<property name="ccc" ref="cBean" />
My XML is now simplified to this:
<bean id="bBean" class="com.xxx.B" />
<bean id="cBean" class="com.xxx.C" />
<bean id="aBean" class="com.yyy.A" />
When I load the context I get the following output:
creating bean B: com.xxx.B@5e5a50
creating bean C: com.xxx.C@54a328
creating bean A: com.yyy.A@a3d4cf
OK, this is wrong! What happened? Why aren’t my properties autowired?
Well, annotations are a nice feature but by themselves they do nothing whatsoever. They just annotate stuff. You need a processing tool to find the annotations and do something with them.
<context:annotation-config> to the rescue. This activates the actions for the annotations that it finds on the beans defined in the same application context where itself is defined.
If I change my XML to this:
<context:annotation-config />
<bean id="bBean" class="com.xxx.B" />
<bean id="cBean" class="com.xxx.C" />
<bean id="aBean" class="com.yyy.A" />
when I load the application context I get the proper result:
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, this is nice, but I’ve removed two rows from the XML and added one. That’s not a very big difference. The idea with annotations is that it’s supposed to remove the XML.
So let’s remove the XML definitions and replace them all with annotations:
package com.
import org.
@Component
public class B {
public B() {
System.out.println("creating bean B: " + this);
package com.
import org.
@Component
public class C {
public C() {
System.out.println("creating bean C: " + this);
package com.
import org.springframework.beans.factory.annotation.A
import org.
import com.xxx.B;
import com.xxx.C;
@Component
public class A {
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 =
@Autowired
public void setCcc(C ccc) {
System.out.println("setting A.ccc with " + ccc);
this.ccc =
While in the XML we only keep this:
<context:annotation-config />
We load the context and the result is… Nothing. No beans are created, no beans are autowired. Nothing!
That’s because, as I said in the first paragraph, the <context:annotation-config /> only works on beans registered within the application context. Because I removed the XML configuration for the three beans there is no bean created and <context:annotation-config /> has no “targets” to work on.
But that won’t be a problem for <context:component-scan> which can scan a package for “targets” to work on. Let’s change the content of the XML config into the following entry:
<context:component-scan base-package="com.xxx" />
When I load the context I get the following output:
creating bean B: com.xxx.B@1be0f0a
creating bean C: com.xxx.C@80d1ff
Hmmmm… something is missing. Why?
If you look closelly at the classes, class A has package com.yyy but I’ve specified in the <context:component-scan> to use package com.xxx so this completely missed my A class and only picked up B and C which are on the com.xxx package.
To fix this, I add this other package also:
<context:component-scan base-package="com.xxx,com.yyy" />
and now we get the expected result:
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
And that’s it! Now you don’t have XML definitions anymore, you have annotations.
As a final example, keeping the annotated classes A, B and C and adding the following to the XML, what will we get after loading the context?
<context:component-scan base-package="com.xxx" />
<bean id="aBean" class="com.yyy.A" />
We still get the correct result:
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
Even if the bean for class A isn’t obtained by scanning, the processing tools are still applied by <context:component-scan> on all beans registered in the application context, even for A which was manually registered in the XML.
But what if we have the following XML, will we get duplicated beans because we’ve specified both <context:annotation-config /> and <context:component-scan>?
<context:annotation-config />
<context:component-scan base-package="com.xxx" />
<bean id="aBean" class="com.yyy.A" />
No, no duplications, We again get the expected result:
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
That’s because both tags register the same processing tools (<context:annotation-config /> can be omitted if <context:component-scan> is specified) but Spring takes care of running them only once.
Even if you register the processing tools yourself multiple times, Spring will still make sure they do t this XML:
<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" />
will still generate the following result:
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
OK, that about raps it up.
注意:本文中所有的”<”和”>”都是中文标点;因为如果是英文半角的话,在该博客上将无法显示;
本来我是打算翻译一下的,但现在看来已经不用这么麻烦了,因为有些前辈已经早先一步了:
我的热门文章博客分类:
&annotaion-driven/&标签:
这个标签对应的实现类是org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
仔细阅读它的注释文档可以很明显的看到这个类的作用。解析这个文档:
这个类主要注册8个类的实例:
1.RequestMappingHandlerMapping
2.BeanNameUrlHandlerMapping
3.RequestMappingHandlerAdapter
4.HttpRequestHandlerAdapter
5.SimpleControllerHandlerAdapter
6.ExceptionHandlerExceptionResolver
7.ResponseStatusExceptionResolver
8.DefaultHandlerExceptionResolver
1是处理@RequestMapping注解的,2.将controller类的名字映射为请求url。1和2都实现了HandlerMapping接口,用来处理请求映射。
3是处理@Controller注解的控制器类,4是处理继承HttpRequestHandlerAdapter类的控制器类,5.处理继承SimpleControllerHandlerAdapter类的控制器。所以这三个是用来处理请求的。具体点说就是确定调用哪个controller的哪个方法来处理当前请求。
6,7,8全部继承AbstractHandlerExceptionResolver,这个类实现HandlerExceptionResolver,该接口定义:接口实现的对象可以解决处理器映射、执行期间抛出的异常,还有错误的视图。
所以&annotaion-driven/&标签主要是用来帮助我们处理请求映射,决定是哪个controller的哪个方法来处理当前请求,异常处理。
&context:component-scan/&标签:
它的实现类是org.springframework.ponentScanBeanDefinitionParser.
把鼠标放在context:component-scan上就可以知道有什么作用的,用来扫描该包内被@Repository @Service @Controller的注解类,然后注册到工厂中。并且context:component-scan激活@ required。@ resource,@ autowired、@PostConstruct @PreDestroy @PersistenceContext @PersistenceUnit。使得在适用该bean的时候用@Autowired就行了。
转自:http://blog.csdn.net/sunhuwh/article/details/
浏览: 128074 次
来自: 北京
volatile并不保证原子性吧
五、以上规则对其它对象锁同样适用:这个好像不太好理解?不过讲的 ...
太谢谢你了、
给LZ赞一个!
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'Spring MVC: &context:annotation-config& 和 &context:component-scan&区别 - CSDN博客
Spring MVC: &context:annotation-config& 和 &context:component-scan&区别
在这篇文章中,我将介绍&context:annotation-config&和&context:component-scan&标签的区别,将来再使用它们的时候能够清楚的知道你在干什么
首先,&context:annotation-config&是用来激活已经在application context中注册的bean。注意,不管你使用那种注册的方式都是可以的,比如使用&context:component-scan&或者在application-context.xml中定义。
第二个区别是是由第一个差异导致的,它确实在context中注册beans,它也在beans内扫描注解并且激活他们,所以&context:component-scan&做了&context:annotation-config&要做的事情,&context:component-scan&还要扫描packages并且在application context中注册扫描的beans.
使用&context:annotation-config&和&context:component-scan&的例子:
我将以例子的方式更加直观的方式介绍这两个标签,为了保持例子的简洁性我只创建了3个beans,然后我将使用多种方式在配置文件中配置他们,然后我们可以在控制台上根据他们的输出结果看到他们的不同。
下边三个beans,BeanA引用了BeanBag和BeanC。
package com.howtodoinjava.
import org.springframework.beans.factory.annotation.A
import org.
@SuppressWarnings(&unused&)
@Component
public class BeanA {
private BeanB beanB;
private BeanC beanC;
public BeanA(){
System.out.println(&Creating bean BeanA&);
@Autowired
public void setBeanB(BeanB beanB) {
System.out.println(&Setting bean reference for BeanB&);
this.beanB = beanB;
@Autowired
public void setBeanC(BeanC beanC) {
System.out.println(&Setting bean reference for BeanC&);
this.beanC = beanC;
package com.howtodoinjava.
import org.
@Component
public class BeanB {
public BeanB(){
System.out.println(&Creating bean BeanB&);
package com.howtodoinjava.
import org.
@Component
public class BeanC {
public BeanC(){
System.out.println(&Creating bean BeanC&);
BeanDemo这个类被用来加载和初始化这个application context
package com.howtodoinjava.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class BeanDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(&classpath:beans.xml&);
现在让我们开始用不同的方式写配置文件bean.xml,我在下边的这些文件中省略模式声明的部分,来关注配置的本身。
&bean id=&beanA& class=&com.howtodoinjava.beans.BeanA&&&/bean&
&bean id=&beanB& class=&com.howtodoinjava.beans.BeanB&&&/bean&
&bean id=&beanC& class=&com.howtodoinjava.beans.BeanC&&&/bean&
Creating bean BeanA
Creating bean BeanB
Creating bean BeanC
在这个例子中,所有的3个beans都被创建了,但是BeanA中的依赖没有被注入,因为我们没有使用任何property/ref 属性
&bean id=&beanA& class=&com.howtodoinjava.beans.BeanA&&
&property name=&beanB& ref=&beanB&&&/property&
&property name=&beanC& ref=&beanC&&&/property&
&bean id=&beanB& class=&com.howtodoinjava.beans.BeanB&&&/bean&
&bean id=&beanC& class=&com.howtodoinjava.beans.BeanC&&&/bean&
Creating bean BeanA
Creating bean BeanB
Creating bean BeanC
Setting bean reference for BeanB
Setting bean reference for BeanC
现在毫无疑问,所有的bean都被注入了
仅仅使用&context:annotation-config&
&context:annotation-config /&
//No Output
像我刚才已经说到的,&context:annotation-config&仅仅激活那些已经发现和注册了的beans,这里我们没有发现任何bean所以什么也不会发生
使用&context:annotation-config&和bean的声明
&context:annotation-config /&
&bean id=&beanA& class=&com.howtodoinjava.beans.BeanA&&&/bean&
&bean id=&beanB& class=&com.howtodoinjava.beans.BeanB&&&/bean&
&bean id=&beanC& class=&com.howtodoinjava.beans.BeanC&&&/bean&
Creating bean BeanA
Creating bean BeanB
Setting bean reference for BeanB
Creating bean BeanC
Setting bean reference for BeanC
在上边的配置中,阀门通过bean标签来发现beans,现在我们使用&context:annotation-config&仅仅激活@Atuowired注解,BeanA中bean的注入发生。
E:仅仅使用&context:component-scan&
&context:component-scan base-package=&com.howtodoinjava.beans& /&
Creating bean BeanA
Creating bean BeanB
Setting bean reference for BeanB
Creating bean BeanC
Setting bean reference for BeanC
上边的代码实现了我在前边提到的两种事情,第一:发现bean(在包中查找@Component标签),第二激活额外的标签(例如@Atuowired)
F:同时使用&context:component-scan&和&context:annotation-config&
&context:annotation-config /&
&context:component-scan base-package=&com.howtodoinjava.beans& /&
&bean id=&beanA& class=&com.howtodoinjava.beans.BeanA&&&/bean&
&bean id=&beanB& class=&com.howtodoinjava.beans.BeanB&&&/bean&
&bean id=&beanC& class=&com.howtodoinjava.beans.BeanC&&&/bean&
Creating bean BeanA
Creating bean BeanB
Setting bean reference for BeanB
Creating bean BeanC
Setting bean reference for BeanC
奇怪的是,使用上边的配置我们发现和激活注解都是两次,但是输出的结果只有一次,这是因为spring很聪明的值注册了一次bean。
现在我希望在你使用这些的时候已经给了你一个清晰的轮廓。
原文地址://spring-mvc-difference-between-contextannotation-config-vs-contextcomponent-scan/
本文已收录于以下专栏:
相关文章推荐
/problems/66133
相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAd...
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
  使用@Autowired注解,必须事先在Spring容器中声明Autowire...
javascript正则表达式定义(语法)
正则表达式的2种定义方法:一种是直接调用RegExp(),第二种是直接用字面量来定义,即var re = /正则规则/;
2种定义方法本质都是调用R...
在基于主机方式配置Spring的配置文件中,你可能会见到这样一条配置,他的作用是式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、Common...
是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。
除了具有的功能之外,还可以在指定的package下扫描以及注册...
Spring配置中的
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
  使用@Autowired注解,必须事先在Spring容器中声明Autowir...
javaScript及jquery的代码跟踪及调试
没有implements Serializable,你就不能通过rmi(包括ejb)提供远程调用。 serialization 允许你将实现了Serializable接口的对象转换为字节序列,这些字节...
Difference between
 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package
sanning的方式)上...
他的最新文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 componentscan不扫描 的文章

 

随机推荐