ゞ沉默是金ゞ
魚離不開水,但是沒有說不離開哪滴水.
BlogJava
首頁
新隨筆
新文章
聯(lián)系
聚合
管理
posts - 98,comments - 104,trackbacks - 0
<
2009年4月
>
日
一
二
三
四
五
六
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
Ant
Broker(1)
DB(8)
EJB(2)
eXtreme(7)
Hibernate
HTTP(6)
Java EE(8)
Java SE(31)
Jrules
Maven
Spring
Spring基礎(chǔ)(1)
Struts
webservice(2)
其他收集(6)
性能測試(1)
搜索引擎
面試相關(guān)
隨筆檔案
2012年12月 (1)
2012年11月 (1)
2012年8月 (16)
2012年7月 (6)
2012年6月 (11)
2011年3月 (1)
2010年12月 (1)
2010年11月 (2)
2010年8月 (1)
2010年7月 (3)
2010年5月 (1)
2010年1月 (1)
2009年12月 (3)
2009年11月 (2)
2009年8月 (1)
2009年6月 (4)
2009年5月 (1)
2009年4月 (1)
2009年3月 (6)
2009年2月 (3)
2008年12月 (4)
文章分類
EJB(1)
代碼收集
實用腳本
文章檔案
2012年6月 (2)
2012年1月 (1)
2011年8月 (1)
2009年11月 (1)
2009年3月 (1)
2009年2月 (2)
搜索
最新評論
1.?re: How to Create Client/Server Keystores using Java Keytool
Shawn?
--Freud
2.?re: HTTP/1.1 Cache-Control的理解
這是網(wǎng)頁代碼,還是IIS 代碼
--marmoglass
3.?re: How To Avoid javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated Problem Using Apache HttpClient
評論內(nèi)容較長,點擊標(biāo)題查看
--dashi99
4.?re: How To Avoid javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated Problem Using Apache HttpClient
評論內(nèi)容較長,點擊標(biāo)題查看
--shigangxing
5.?re: Part 12: java.util.concurrent : SingleThreadPool Example
評論內(nèi)容較長,點擊標(biāo)題查看
--NotNolyJava
閱讀排行榜
1.?HTTP/1.1 Cache-Control的理解(10104)
2.?MYSQL 連接數(shù)據(jù)庫命令收藏(8624)
3.?什么是RFC?(6619)
4.?can not be represented as java.sql.Timestamp(5956)
5.?How To Avoid javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated Problem Using Apache HttpClient(3651)
評論排行榜
1.?Java應(yīng)用基礎(chǔ):網(wǎng)友精選25個Java試題分享(18)
2.?踏實與成功(11)
3.?Eclipse中10個比較有用的快捷鍵組合(轉(zhuǎn))(7)
4.?摘:一個Java程序員應(yīng)該掌握的10項技能(6)
5.?Java獲取客戶端真實IP地址的兩種方法(5)
Spring自動裝配的學(xué)習(xí)
在xml配置文件中,
autowire
有6種類型,可以在<bean/>元素中使用
autowire
屬性指定:
模式 說明
no 不使用自動裝配,必須通過ref元素指定依賴,默認(rèn)設(shè)置。
byName 根據(jù)屬性名自動裝配。此選項將檢查容器并根據(jù)名字查找與
屬性完全一致的bean,并將其與屬性自動裝配。
byType 如果容器中存在一個與指定屬性類型相同的bean,那么將與
該屬性自動裝配;如果存在多個該類型bean,那么拋出異
常,并指出不能使用byType方式進行自動裝配;如果沒有找
到相匹配的bean,則什么事都不發(fā)生,也可以通過設(shè)置
dependency
-
check
=
"
objects
"
讓Spring拋出異常。
constructor 與byType方式類似,不同之處在于它應(yīng)用于構(gòu)造器參數(shù)。如
果容器中沒有找到與構(gòu)造器參數(shù)類型一致的bean,那么拋出
異常。
autodetect 通過bean類的自省機制(introspection)來決定是使用
constructor還是byType方式進行自動裝配。如果發(fā)現(xiàn)默認(rèn)的
構(gòu)造器,那么將使用byType方式。
default
看代碼:
Computer.java
package
com.redoffice;
public
class
Computer
{
private
Host host;
private
Display display;
public
Computer()
{
}
public
Computer(Host host, Display display)
{
super
();
this
.host
=
host;
this
.display
=
display;
}
public
void
run()
{
System.out.println();
System.out.print(
"
"
+
host.run()
+
"
,
"
);
System.out.println(display.run());
}
public
Host getHost()
{
return
host;
}
public
void
setHost(Host host)
{
this
.host
=
host;
}
public
Display getDisplay()
{
return
display;
}
public
void
setDisplay(Display display)
{
this
.display
=
display;
}
}
Host.java
package
com.redoffice;
public
class
Host
{
public
String run()
{
return
"
我是主機,正在運行!
"
;
}
}
Display.java
package
com.redoffice;
public
class
Display
{
public
String run()
{
return
"
我是顯示器,正在運行!
"
;
}
}
TestMain.java
package
com.redoffice;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.log4j.PropertyConfigurator;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
TestMain
{
private
static
Log log
=
LogFactory.getLog(TestMain.
class
);
public
static
void
main(String[] args)
{
PropertyConfigurator.configure(
"
bin/log4j.properties
"
);
log.debug(
"
entering main method
"
);
ApplicationContext ac
=
new
ClassPathXmlApplicationContext(
"
applicationContext.xml
"
);
//
byName
Computer computer1
=
(Computer)ac.getBean(
"
computer1
"
);
System.out.println(
"
autowire=\
"
byName\
"
:
"
);
computer1.run();
//
byType
Computer computer2
=
(Computer)ac.getBean(
"
computer2
"
);
System.out.println(
"
autowire=\
"
byType\
"
:
"
);
computer2.run();
//
default
Computer computer3
=
(Computer)ac.getBean(
"
computer3
"
);
System.out.println(
"
autowire=\
"
default
\
"
:
"
);
computer3.run();
//
autodetect
Computer computer4
=
(Computer)ac.getBean(
"
computer4
"
);
System.out.println(
"
autowire=\
"
autodetect\
"
:
"
);
computer4.run();
//
constructor
Computer computer5
=
(Computer)ac.getBean(
"
computer5
"
);
System.out.println(
"
autowire=\
"
constructor\
"
:
"
);
computer5.run();
//
no
Computer computer6
=
(Computer)ac.getBean(
"
computer6
"
);
System.out.println(
"
autowire=\
"
no\
"
:
"
);
computer6.run();
log.debug(
"
leaving main method
"
);
}
}
applicationContext.xml
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
beans xmlns
=
"
http://www.springframework.org/schema/beans
"
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
xsi:schemaLocation
=
"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
"
default
-
autowire
=
"
autodetect
"
>
<
bean id
=
"
host
"
name
=
"
host
"
class
=
"
com.redoffice.Host
"
></
bean
>
<
bean id
=
"
display
"
name
=
"
display
"
class
=
"
com.redoffice.Display
"
></
bean
>
<!--
autowire有no,constructor,byName,byType,
default
,autodetect幾種狀況
-->
<
bean id
=
"
computer1
"
class
=
"
com.redoffice.Computer
"
autowire
=
"
byName
"
></
bean
>
<
bean id
=
"
computer2
"
class
=
"
com.redoffice.Computer
"
autowire
=
"
byType
"
></
bean
>
<
bean id
=
"
computer3
"
class
=
"
com.redoffice.Computer
"
autowire
=
"
default
"
></
bean
>
<
bean id
=
"
computer4
"
class
=
"
com.redoffice.Computer
"
autowire
=
"
autodetect
"
></
bean
>
<!--
在Computer類中必須有一個無參和有參的構(gòu)造函數(shù),否則報錯
-->
<
bean id
=
"
computer5
"
class
=
"
com.redoffice.Computer
"
autowire
=
"
constructor
"
>
<
constructor
-
arg index
=
"
0
"
>
<
ref bean
=
"
host
"
/>
</
constructor
-
arg
>
<
constructor
-
arg index
=
"
1
"
>
<
ref bean
=
"
display
"
/>
</
constructor
-
arg
>
</
bean
>
<
bean id
=
"
computer6
"
class
=
"
com.redoffice.Computer
"
autowire
=
"
no
"
>
<
constructor
-
arg index
=
"
0
"
>
<
ref bean
=
"
host
"
/>
</
constructor
-
arg
>
<
constructor
-
arg index
=
"
1
"
>
<
ref bean
=
"
display
"
/>
</
constructor
-
arg
>
</
bean
>
</
beans
>
希望有什么不對的地方多多指教。
posted on 2009-04-03 10:28
ゞ沉默是金ゞ
閱讀(1716)
評論(4)
編輯
收藏
所屬分類:
Spring基礎(chǔ)
FeedBack:
#
re: Spring自動裝配的學(xué)習(xí)
2009-04-03 10:35 |
asiawang
自動裝配 還是不用為好,會產(chǎn)生不可預(yù)知的錯誤!
回復(fù)
更多評論
#
re: Spring自動裝配的學(xué)習(xí)
2009-04-03 11:41 |
.........
是啊.
回復(fù)
更多評論
#
re: Spring自動裝配的學(xué)習(xí)
2009-04-04 18:03 |
Robin's Java World
@asiawang
我們在項目里用得蠻好的呀,不過最好只用byName方式。
回復(fù)
更多評論
#
re: Spring自動裝配的學(xué)習(xí)[未登錄]
2010-08-22 13:10 |
Sam
寫的簡潔易懂。
其實第五點,類沒有顯式寫出構(gòu)造函數(shù)的話,java不會再創(chuàng)造默認(rèn)構(gòu)造函數(shù)。所以你必須寫明無參構(gòu)造函數(shù)。
很有用處
回復(fù)
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Copyright ©2025 ゞ沉默是金ゞ Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
县级市
|
沁水县
|
磐石市
|
沁阳市
|
侯马市
|
安乡县
|
兴文县
|
安塞县
|
和平区
|
庆安县
|
高青县
|
淮南市
|
张家港市
|
泗洪县
|
巴彦淖尔市
|
河间市
|
朝阳区
|
巴青县
|
上林县
|
七台河市
|
德令哈市
|
平江县
|
满城县
|
恩施市
|
南汇区
|
通海县
|
黄浦区
|
龙山县
|
福鼎市
|
柳江县
|
海伦市
|
丹江口市
|
永年县
|
安庆市
|
宁津县
|
斗六市
|
高邑县
|
洛浦县
|
SHOW
|
西吉县
|
乃东县
|