久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

Spring中怎么通過實(shí)例工廠方法創(chuàng)建Bean-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Spring中怎么通過實(shí)例工廠方法創(chuàng)建Bean,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)興和免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

一 配置

<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="/tupian/20230522/"   xsi:schemaLocation="/tupian/20230522/   /tupian/20230522//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()方法來創(chuàng)建Bean   該bean元素包含的constructor-arg元素用于為工廠方法指定參數(shù),   因此這段配置會(huì)驅(qū)動(dòng)Spring以反射方式來執(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以反射方式來執(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 + ",下次再見";   }}

四 工廠類

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();    }  }}

五 測試類

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"));  }}

六 測試

Mary,您好Mary,下次再見Jack,Hello!Jack,Good Bye!

關(guān)于Spring中怎么通過實(shí)例工廠方法創(chuàng)建Bean就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:Spring中怎么通過實(shí)例工廠方法創(chuàng)建Bean-創(chuàng)新互聯(lián)
鏈接地址:http://sd-ha.com/article4/shpie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)建站、網(wǎng)站內(nèi)鏈網(wǎng)站收錄、做網(wǎng)站建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營