這篇文章給大家介紹Spring中調(diào)用實(shí)例工廠方法創(chuàng)建Bean,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)建站專注于網(wǎng)站建設(shè)|成都網(wǎng)站維護(hù)公司|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋陽(yáng)光房等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身定制品質(zhì)網(wǎng)站。
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置工廠Bean,該Bean負(fù)責(zé)產(chǎn)生其他Bean實(shí)例 --> <bean id="personFactory" class="org.crazyit.app.factory.PersonFactory"/> <!-- 下面配置驅(qū)動(dòng)Spring調(diào)用personFactory Bean的getPerson()方法來(lái)創(chuàng)建Bean 該bean元素包含的constructor-arg元素用于為工廠方法指定參數(shù), 因此這段配置會(huì)驅(qū)動(dòng)Spring以反射方式來(lái)執(zhí)行如下代碼: PersonFactory pf = container.get("personFactory"); // container代表Spring容器 chinese = pf.getPerson("chin"); --> <bean id="chinese" factory-bean="personFactory" factory-method="getPerson"> <!-- 配置實(shí)例工廠方法的參數(shù) --> <constructor-arg value="chin"/> </bean> <!-- 下面配置會(huì)驅(qū)動(dòng)Spring以反射方式來(lái)執(zhí)行如下代碼: PersonFactory pf = container.get("personFactory"); // container代表Spring容器 american = pf.getPerson("ame"); --> <bean id="american" factory-bean="personFactory" factory-method="getPerson"> <constructor-arg value="ame"/> </bean> </beans>
二 接口
Person
package org.crazyit.app.service; public interface Person { // 定義一個(gè)打招呼的方法 public String sayHello(String name); // 定義一個(gè)告別的方法 public String sayGoodBye(String name); }
三 實(shí)現(xiàn)類
American
public class American implements Person { // 實(shí)現(xiàn)Person接口必須實(shí)現(xiàn)如下兩個(gè)方法 public String sayHello(String name) { return name + ",Hello!"; } public String sayGoodBye(String name) { return name + ",Good Bye!"; } }
Chinese
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class Chinese implements Person { // 實(shí)現(xiàn)Person接口必須實(shí)現(xiàn)如下兩個(gè)方法 public String sayHello(String name) { return name + ",您好"; } public String sayGoodBye(String name) { return name + ",下次再見(jiàn)"; } }
四 工廠類
package org.crazyit.app.factory; import org.crazyit.app.service.*; import org.crazyit.app.service.impl.*; public class PersonFactory { // 獲得Person實(shí)例的實(shí)例工廠方法 // ethnic參數(shù)決定返回哪個(gè)Person實(shí)現(xiàn)類的實(shí)例 public Person getPerson(String ethnic) { if (ethnic.equalsIgnoreCase("chin")) { return new Chinese(); } else { return new American(); } } }
五 測(cè)試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class SpringTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person p1 = ctx.getBean("chinese" , Person.class); System.out.println(p1.sayHello("Mary")); System.out.println(p1.sayGoodBye("Mary")); Person p2 = ctx.getBean("american" , Person.class); System.out.println(p2.sayHello("Jack")); System.out.println(p2.sayGoodBye("Jack")); } }
六 測(cè)試
Mary,您好
Mary,下次再見(jiàn)
Jack,Hello!
Jack,Good Bye!
關(guān)于Spring中調(diào)用實(shí)例工廠方法創(chuàng)建Bean就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:Spring中調(diào)用實(shí)例工廠方法創(chuàng)建Bean
轉(zhuǎn)載源于:http://www.rwnh.cn/article34/gjhipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站制作、商城網(wǎng)站、靜態(tài)網(wǎng)站、App設(shè)計(jì)、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)