四、創(chuàng)建Bean的三種方式

使用構(gòu)造器創(chuàng)建Bean實(shí)例(如前所示)

使用靜態(tài)工廠方法創(chuàng)建實(shí)例:

Being.java

package inter;

public interface Being {

    public void testBeing();
}

Cat.java

package entity;

import inter.Being;

public class Cat implements Being{
    private String msg;
    public void setMsg(String msg)
    {
        this.msg=msg;
    }
    @Override
    public void testBeing() {
        // TODO Auto-generated method stub
        System.out.println(msg+",愛(ài)吃老鼠!");
    }

}

Dog.java

package entity;

import inter.Being;

public class Dog implements Being{

    private String msg;
    public void setMsg(String msg)
    {
        this.msg=msg;
    }
    @Override
    public void testBeing() {
        // TODO Auto-generated method stub
        System.out.println(msg+",愛(ài)啃骨頭!");
    }

    
}

BeanFactory.java

package config;

import entity.Cat;
import entity.Dog;
import inter.Being;

public class BeingFactory {

    //返回Being實(shí)例的靜態(tài)工廠方法
    public static Being getBeing(String msg)
    {

        if(msg.equalsIgnoreCase("dog"))
        {
            return new Dog();
        }
        else
        {
            return new Cat();
        }
    }
}

beans.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
        <bean id="dog" class="config.BeingFactory" factory-method="getBeing">
          <!-- 配置靜態(tài)工廠方法的參數(shù) -->
          <constructor-arg value="dog"/>
          <!-- 驅(qū)動(dòng)Spring以“狗”為參數(shù)來(lái)執(zhí)行dog的setMsg()方法  -->
          <property name="msg" value="狗" />
        </bean>
        <bean id="cat" class="config.BeingFactory" factory-method="getBeing">
          <!-- 配置靜態(tài)工廠方法的參數(shù) -->
          <constructor-arg value="cat"/>
          <!-- 驅(qū)動(dòng)Spring以“貓”為參數(shù)來(lái)執(zhí)行dog的setMsg()方法 -->
          <property name="msg" value="貓" />
        </bean>
</beans>

采用靜態(tài)工廠方法來(lái)生產(chǎn)Bean實(shí)例需要指定兩個(gè)屬性:

  • class:該屬性值為靜態(tài)工廠類(lèi)的類(lèi)名。
  • factory-method:該屬性指定靜態(tài)工廠方法生產(chǎn)來(lái)生產(chǎn)Bean實(shí)例。

使用靜態(tài)工廠方法是必須指定靜態(tài)工廠類(lèi),工廠類(lèi)包含產(chǎn)生實(shí)例的靜態(tài)方法。

  • class屬性不再是Bean實(shí)例的實(shí)現(xiàn)類(lèi),而是生成Bean實(shí)例的靜態(tài)工廠類(lèi)。
  • 使用factory-method屬性指定創(chuàng)建Bean實(shí)例的靜態(tài)工廠方法。
  • 如果靜態(tài)工廠方法需要參數(shù),則使用<constructor-arg.../>元素來(lái)指定靜態(tài)工廠方法的參數(shù)。

調(diào)用實(shí)例工廠方法創(chuàng)建Bean:

工廠方法的區(qū)別:

package config;

import entity.Cat;
import entity.Dog;
import inter.Being;

public class BeingFactory {

    //返回Being實(shí)例的靜態(tài)工廠方法
    public Being getBeing(String msg)
    {

        if(msg.equalsIgnoreCase("dog"))
        {
            return new Dog();
        }
        else
        {
            return new Cat();
        }
    }
}

配置文件的差別:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
       <!-- 配置工廠Bean,該Bean負(fù)責(zé)產(chǎn)生其他的Bean -->
       <bean id="beingFactory" class="config.BeingFactory"/>
       
       <bean id="dog" class="entity.Dog" factory-bean="beingFactory" factory-method="getBeing">
         <constructor-arg value="dog"/>
         <property name="msg" value="狗"/>
       </bean>
       
        <bean id="cat" class="entity.Cat" factory-bean="beingFactory" factory-method="getBeing">
         <constructor-arg value="cat"/>
         <property name="msg" value="貓"/>
       </bean>
</beans>

靜態(tài)工廠方法和實(shí)例工廠方法的異同:

  • 配置實(shí)例工廠創(chuàng)建Bean,必須將實(shí)例工廠配置成Bean實(shí)例;而配置靜態(tài)工廠創(chuàng)建Bean,則無(wú)需配置工廠Bean。
  • 配置實(shí)例工廠方法創(chuàng)建Bean必須使用factory-bean屬性確定工廠Bean;配置靜態(tài)工廠方法創(chuàng)建Bean,使用class元素確定靜態(tài)工廠類(lèi)。
  • 都需要使用factory-method屬性指定產(chǎn)生Bean實(shí)例的工廠方法。
  • 工廠方法如果需要參數(shù),都是用<constructor-arg.../>元素指定參數(shù)值。
  • 普通的設(shè)值注入都使用<property.../>元素確定參數(shù)。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評(píng)論 19 139
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,206評(píng)論 2 7
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,970評(píng)論 6 342
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 31,767評(píng)論 18 399
  • 文章作者:Tyan博客:noahsnail.com 3.4 依賴(lài) 標(biāo)準(zhǔn)企業(yè)應(yīng)用不會(huì)由一個(gè)對(duì)象(或Spring用語(yǔ)中...
    SnailTyan閱讀 1,210評(píng)論 0 1