<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);}@Autowiredpublic void setBbb(B bbb) {System.out.println("setting A.bbb with " + bbb);this.bbb = bbb;}@Autowiredpublic 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);}@Autowiredpublic void setBbb(B bbb) {System.out.println("setting A.bbb with " + bbb);this.bbb = bbb;}@Autowiredpublic 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 />
.
?
?