前言
Spring框架對(duì)Bean進(jìn)行裝配提供了很靈活的方式,下面歸納一下主要的方式:
- 在XML中進(jìn)行顯示配置
- 在Java中進(jìn)行顯示配置
- 隱式的bean發(fā)現(xiàn)機(jī)制和自動(dòng)裝配
而自動(dòng)裝配實(shí)現(xiàn)就需要注解掃描,這時(shí)發(fā)現(xiàn)了兩種開啟注解掃描的方式,即<context:annotation-config/>和<context:component-scan>
下面歸納一下這兩種方式的異同點(diǎn):
<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過的Bean
<context:component-scan>:不僅具備<context:annotation-config>的所有功能,還可以在指定的package下面掃描對(duì)應(yīng)的bean
Demo:
Demo:XML注冊(cè)Bean方式
下面給出兩個(gè)類,類A和類B
1
2
3
4
5
6
7
|
package com.test; pubic class B{ public B(){ System.out.println( "B類" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.test; public class A { private B bClass; public void setBClass(B bClass){ this .bClass = bClass; System.out.println( "通過set的方式注入B類" ); } public A(){ System.out.println( "A類" ); } } |
如何我們這時(shí)可以通過傳統(tǒng)的xml配置在Application.xml里進(jìn)行bean注冊(cè)
1
2
3
4
|
< bean id = "bBean" class = "com.test.B" /> < bean id = "aBean" class = "com.test.A" > < property name = "bClass" ref = "bBean" /> </ bean > |
啟動(dòng)加載Application.xml
輸出:
類B
類A
通過set的方式注入B類
Demo:annotation配置注解開啟方式
1
2
3
4
5
6
7
|
package com.test; pubic class B{ public B(){ System.out.println( "B類" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.test; public class A { private B bClass; @Autowired public void setBClass(B bClass){ this .bClass = bClass; System.out.println( "通過set的方式注入B類" ); } public A(){ System.out.println( "A類" ); } } |
然后我們需要在Application.xml里注冊(cè)Bean,假如我們先這樣配置,僅僅注冊(cè)Bean,不開啟掃描
1
2
|
< bean id = "bBean" class = "com.test.B" /> < bean id = "aBean" class = "com.test.A" /> |
或者僅僅開啟掃描,不注冊(cè)Bean
1
|
< context:annotation-config /> |
這時(shí)加載Application.xml配置
輸出:
類B
類A
我們會(huì)發(fā)現(xiàn)下面的@Autowired的方法是不能被加載的
1
2
3
4
5
|
@Autowired public void setBClass(B bClass){ this .bClass = bClass; System.out.println( "通過set的方式注入B類" ); } |
解決方法:
修改Application.xml配置文件
1
2
3
|
< context:annotation-config /> < bean id = "bBean" class = "com.test.B" /> < bean id = "aBean" class = "com.test.A" /> |
重新加載配置文件,輸出正常了
輸出:
類B
類A
通過set的方式注入B類
歸納:<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過的Bean
Demo:component配置注解開啟方式
1
2
3
4
5
6
7
|
package com.test; pubic class B{ public B(){ System.out.println( "B類" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.test; @Component public class A { private B bClass; @Autowired public void setBClass(B bClass){ this .bClass = bClass; System.out.println( "通過set的方式注入B類" ); } public A(){ System.out.println( "A類" ); } } |
然后我們配置一下application.xml,開啟annotaion-config掃描
1
|
< context:annotation-config /> |
加載配置文件:
輸出:
類B
類A
原因:<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過的Bean,Bean并沒有注冊(cè)過,所以即使開啟了@Autowired、@Component注解 和配置開啟了annotaion-config掃描還是加載不到
修改配置文件:
1
|
< context:component-scan base-package = "com.test" /> |
重新加載配置文件:
輸出:
類B
類A
通過set的方式注入B類
歸納:
<context:annotation-config>:注解掃描是針對(duì)已經(jīng)在Spring容器里注冊(cè)過的Bean
<context:component-scan>:不僅具備<context:annotation-config>的所有功能,還可以在指定的package下面掃描對(duì)應(yīng)的bean
<context:annotation-config />和 <context:component-scan>同時(shí)存在的時(shí)候,前者會(huì)被忽略。
即使注冊(cè)Bean,同時(shí)開啟<context:annotation-config />掃描,@autowire,@resource等注入注解只會(huì)被注入一次,也即只加載一次
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.jianshu.com/p/cd0091aacf89