學(xué)Spring的歷程(2)——Bean的實(shí)例化

一当悔、構(gòu)造器實(shí)例化

構(gòu)造器實(shí)例化是指Spring容器通過Bean對應(yīng)類中默認(rèn)的無參構(gòu)造方法來實(shí)例化Bean.

通過一個簡單的例子來訓(xùn)練区丑。

1.在搭建好的項(xiàng)目中的src下創(chuàng)建一個con.spring.constructor包挠轴。在包下創(chuàng)建一個類? ? ? ? ? ? ??Bean1:??

? ?public class Bean1 {??

? ?}

2.在con.spring.constructor包下創(chuàng)建配置文件aplicationContext.xml.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

? ? 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.xsd">

<bean id="bean1" class="alern.spring.constructor.Bean1"></bean>

</beans>

3.創(chuàng)建測試類Test01

package alern.spring.constructor;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {

public static void main(String [] args){

String xmlPath = "alern/spring/constructor/aplicationContext.xml";

ApplicationContext applicationContext =

new ClassPathXmlApplicationContext(xmlPath);

Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");

System.out.println(bean1);

}

}


二诵肛、靜態(tài)工廠方式實(shí)例化

1.?在alern.spring.staticfactory包中創(chuàng)建類Bean2.類中沒有方法

2.在alern.spring.staticfactory包下創(chuàng)建類StaticFactory跑芳,并創(chuàng)建方法返回Bean2得的實(shí)例:

? ? ?public class StaticFactory {

? ? ? public static Bean2 createBean(){

? ? ? return new Bean2();

? ? ?}

? ? ?}

3.在alern.spring.staticfactory包下創(chuàng)建Bean2.xml:

<bean id="bean2" class="alern.spring.staticfactory.StaticFactory"

factory-method="createBean"></bean>

factory-method屬性告訴容器是哪個工廠方法突照。

4.創(chuàng)建測試類Test02:

package alern.spring.staticfactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import alern.spring.constructor.Bean1;

public class Test02 {

public static void main(String [] args){

String xmlPath = "alern/spring/staticfactory/bean2.xml";

ApplicationContext applicationContext =

new ClassPathXmlApplicationContext(xmlPath);

Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");

System.out.println(bean2);

}

}


三帮非、使用實(shí)例工廠方式實(shí)例化

此種方式的工廠類中,不在使用靜態(tài)方法創(chuàng)建Bean實(shí)例,而是直接采用創(chuàng)建Bean實(shí)例的方式讹蘑。同時在配置文件中末盔,也不是由class直接指向?qū)嵗悾峭ㄟ^factory-bean指向配置的實(shí)例工廠座慰,factory-method確定工廠使用的是哪個方法陨舱。

1.在alern.spring.instancefactory包下創(chuàng)建類Bean3,也沒有方法版仔。

2.在alern.spring.instancefactory包下創(chuàng)建類Bean3facotury:

package alern.spring.instancefactory;

public class Bean3facotury {

public Bean3 myfactory(){

return new Bean3();

}

}

3.在alern.spring.instancefactory包下創(chuàng)建bean3,xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

? ? 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.xsd">

<bean id="factory" class="alern.spring.instancefactory.Bean3facotury"></bean>

<!-- 使用 factory-bean屬性指向配置的實(shí)例工廠-->

<!-- 使用factory-method屬性來確定使用工廠的哪個方法 -->

<bean id="bean3" factory-bean="factory" factory-method="myfactory"></bean>

</beans>

4.創(chuàng)建測試類Test3:

package alern.spring.instancefactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test03 {

public static void main(String [] args){

String xmlPath = "alern/spring/instancefactory/bean3.xml";

ApplicationContext applicationContext =

new ClassPathXmlApplicationContext(xmlPath);

Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");

System.out.println(bean3);

}

}


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?學(xué)習(xí)書籍:《javaEE企業(yè)級開發(fā)教程》


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末隅忿,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子邦尊,更是在濱河造成了極大的恐慌背桐,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蝉揍,死亡現(xiàn)場離奇詭異链峭,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)又沾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進(jìn)店門弊仪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來熙卡,“玉大人,你說我怎么就攤上這事励饵〔蛋” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵役听,是天一觀的道長颓鲜。 經(jīng)常有香客問我,道長典予,這世上最難降的妖魔是什么甜滨? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮瘤袖,結(jié)果婚禮上衣摩,老公的妹妹穿的比我還像新娘。我一直安慰自己捂敌,他們只是感情好艾扮,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著占婉,像睡著了一般栏渺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锐涯,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天磕诊,我揣著相機(jī)與錄音,去河邊找鬼纹腌。 笑死霎终,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的升薯。 我是一名探鬼主播莱褒,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼涎劈!你這毒婦竟也來了广凸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤蛛枚,失蹤者是張志新(化名)和其女友劉穎谅海,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蹦浦,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扭吁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片侥袜。...
    茶點(diǎn)故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡蝌诡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出枫吧,到底是詐尸還是另有隱情浦旱,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布九杂,位于F島的核電站颁湖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏尼酿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一植影、第九天 我趴在偏房一處隱蔽的房頂上張望裳擎。 院中可真熱鬧,春花似錦思币、人聲如沸鹿响。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惶我。三九已至,卻和暖如春博投,著一層夾襖步出監(jiān)牢的瞬間绸贡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工毅哗, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留听怕,地道東北人。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓虑绵,卻偏偏與公主長得像尿瞭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子翅睛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容