溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例

發(fā)布時(shí)間:2020-09-13 11:56:15 來源:腳本之家 閱讀:138 作者:cakincqm 欄目:編程語言

本文實(shí)例講述了Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作。分享給大家供大家參考,具體如下:

一 配置

<?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">
   <!-- 下面配置驅(qū)動(dòng)Spring調(diào)用BeingFactory的靜態(tài)getBeing()方法來創(chuàng)建Bean
   該bean元素包含的constructor-arg元素用于為靜態(tài)工廠方法指定參數(shù),
   因此這段配置會(huì)驅(qū)動(dòng)Spring以反射方式來執(zhí)行如下代碼:
   dog = org.crazyit.app.factory.BeingFactory.getBeing("dog"); -->
   <bean id="dog" class="org.crazyit.app.factory.BeingFactory"
      factory-method="getBeing">
      <!-- 配置靜態(tài)工廠方法的參數(shù) -->
      <constructor-arg value="dog"/>
      <!-- 驅(qū)動(dòng)Spring以"我是狗"為參數(shù)來執(zhí)行dog的setMsg()方法 -->
      <property name="msg" value="我是狗"/>
   </bean>
   <!-- 下面配置會(huì)驅(qū)動(dòng)Spring以反射方式來執(zhí)行如下代碼:
   dog = org.crazyit.app.factory.BeingFactory.getBeing("cat"); -->
   <bean id="cat" class="org.crazyit.app.factory.BeingFactory"
      factory-method="getBeing">
      <!-- 配置靜態(tài)工廠方法的參數(shù) -->
      <constructor-arg value="cat"/>
      <!-- 驅(qū)動(dòng)Spring以"我是貓"為參數(shù)來執(zhí)行dog的setMsg()方法 -->
      <property name="msg" value="我是貓"/>
   </bean>
</beans>

二 接口

package org.crazyit.app.service;
public interface Being
{
   public void testBeing();
}

三 實(shí)現(xiàn)類

1 Cat

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class Cat implements Being
{
   private String msg;
   // msg的setter方法
   public void setMsg(String msg)
   {
      this.msg = msg;
   }
   // 實(shí)現(xiàn)接口必須實(shí)現(xiàn)的testBeing方法
   public void testBeing()
   {
      System.out.println(msg +
        ",貓喜歡吃老鼠");
   }
}

2 Dog

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class Dog implements Being
{
   private String msg;
   // msg的setter方法
   public void setMsg(String msg)
   {
      this.msg = msg;
   }
   // 實(shí)現(xiàn)接口必須實(shí)現(xiàn)的testBeing()方法
   public void testBeing()
   {
      System.out.println(msg +
        ",狗愛啃骨頭");
   }
}

四 工廠類

package org.crazyit.app.factory;
import org.crazyit.app.service.impl.*;
import org.crazyit.app.service.*;
public class BeingFactory
{
  // 返回Being實(shí)例的靜態(tài)工廠方法
  // arg參數(shù)決定返回哪個(gè)Being類的實(shí)例
  public static Being getBeing(String arg)
  {
    // 調(diào)用此靜態(tài)方法的參數(shù)為dog,則返回Dog實(shí)例
    if (arg.equalsIgnoreCase("dog"))
    {
      return new Dog();
    }
    // 否則返回Cat實(shí)例
    else
    {
      return new Cat();
    }
  }
}

五 測(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)
  {
    // 以類加載路徑下的配置文件創(chuàng)建ClassPathResource實(shí)例
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    Being b1 = ctx.getBean("dog" , Being.class);
    b1.testBeing();
    Being b2 = ctx.getBean("cat" , Being.class);
    b2.testBeing();
  }
}

六 測(cè)試結(jié)果

我是狗,狗愛啃骨頭
我是貓,貓喜歡吃老鼠

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI