<context:annotation-config> vs <context:component-scan>
<context:annotation-config> 用来注入已经在上下文注册的bean,无论bean是定义在XML中还是被 package scanning。
<context:component-scan>仅scans packages 去注册应用上线文中的Bean。
example:
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.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; } }
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>
加载环境产生输出:
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
这是正常的输出,但是这是老的spring 风格.,现在我们使用注解。
First, lets autowire the bbb
and ccc
properties on bean A
like so:
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; } }
XML配置:
<bean id="bBean" class="com.xxx.B" />
<bean id="cBean" class="com.xxx.C" />
<bean id="aBean" class="com.yyy.A" />
加载结果:
creating bean B: com.xxx.B@5e5a50
creating bean C: com.xxx.C@54a328
creating bean A: com.yyy.A@a3d4cf
上面的结果并没有完成注入,这是因为@Autowired 只是一个注解,如果让它完成注入工作需要<context:annotation-config />
改变一下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" />
输出结果:
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
现在,我们使用注解注释Bean 删除砸xml中的定义:
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; } }
xml中的配置:
<context:annotation-config />
加载后什么都没有输出。
<context:component-scan base-package="com.xxx" />
<context:annotation-config /> <context:component-scan base-package="com.xxx" /> <bean id="aBean" class="com.yyy.A" />
OK,得到了我们想要的结果。
总结:
The difference between the two is really simple!.
<context:annotation-config />
Enables you to use annotations that are restricted to wiring up properties and constructors only of beans!.
Where as
<context:component-scan base-package="org.package"/>
Enables everything that <context:annotation-config />
can do, with addition of using stereotypes eg.. @Component
, @Service
, @Repository
. So you can wire entire beans and not just restricted to constructors or properties!.
<context:annotation-config>
: Scanning and activating annotations for already registered beans in spring config xml.
<context:component-scan>
: Bean registration + <context:annotation-config>
@Autowired and @Required are targets property level so bean should register in spring IOC before use these annotations. To enable these annotations either have to register respective beans or include <context:annotation-config />
. i.e. <context:annotation-config />
works with registered beans only.
@Required enables RequiredAnnotationBeanPostProcessor
processing tool
@Autowired enables AutowiredAnnotationBeanPostProcessor
processing tool
Note: Annotation itself nothing to do, we need a Processing Tool, which is a class underneath, responsible for the core process.
@Repository, @Service and @Controller are @Component, and they targets class level.
<context:component-scan>
it scans the package and find and register the beans, and it includes the work done by <context:annotation-config />
.