天天看點

1.3.2.3. Instantiation by Using an Instance Factory Method (使用執行個體工廠方法進行執行個體化) Spring Framework Documentation (5.3.10)

 Spring Framework Documentation (5.3.10)

Core IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean簡介)

1.2. Container Overview (容器概覽)

1.3. Bean Overview (Bean概覽)

1.3.1. Naming Beans (Bean命名)

1.3.1.1Aliasing a Bean outside the Bean Definition 在Bean定義外指定Bean别名

1.3.2. Instantiating Beans執行個體化bean

1.3.2.1.Instantiation with a Constructor (通過構造函數進行執行個體化)

1.3.2.2. Instantiation with a Static Factory Method (通過靜态工廠方法進行執行個體化)

1.3.2.3. Instantiation by Using an Instance Factory Method (使用執行個體工廠方法進行執行個體化)

1.3.2.4. Determining a Bean’s Runtime Type(确定Bean的運作時類型)

下載下傳此文檔精編完整版

 No. 内容 下載下傳位址 文檔内容目錄
1 中英雙語精編版 第一部分 PDF下載下傳 内容目錄
2 中英雙語精編版 第二部分 PDF下載下傳 内容目錄
3 中文精編版 第一部分 PDF下載下傳 内容目錄
4 中文精編版 第二部分 PDF下載下傳 内容目錄

更多章節内容,請點選檢視:  Core Technologies

1.3.2.3. Instantiation by Using an Instance Factory Method (使用執行個體工廠方法進行執行個體化)

Similar to instantiation through a static factory method, instantiation with an instance factory method invokes a non-static method of an existing bean from the container to create a new bean. To use this mechanism, leave the 

class

 attribute empty and, in the 

factory-bean

 attribute, specify the name of a bean in the current (or parent or ancestor) container that contains the instance method that is to be invoked to create the object. Set the name of the factory method itself with the 

factory-method

 attribute. The following example shows how to configure such a bean:

與通過靜态工廠方法的執行個體化類似,可使用具有執行個體工廠方法的執行個體從容器中調用現有bean的非靜态方法來建立新bean。要使用此機制,請将class屬性設定為空,并在 

factory-bean

屬性中指定目前(或父級或祖先級)容器中的bean名稱,該容器包含被調用以建立對象的執行個體方法。使用

factory-method

 屬性設定工廠方法的名稱。下面的示例示範如何配置這樣的bean:

<!-- the factory bean, which contains a method called createInstance()  -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>
           

The following example shows the corresponding class:

以下示例展示了相應的類:

Java

public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl() ;

    public ClientService createClientServiceInstance()  {
        return clientService;
	}
}
           

Kotlin

class DefaultServiceLocator {
    companion object {
        private val clientService = ClientServiceImpl() 
    }
    fun createClientServiceInstance() : ClientService {
        return clientService
    }
}
           

One factory class can also hold more than one factory method, as the following example shows:

一個工廠類還可以包含多個工廠方法,如下例所示:

<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>

<bean id="accountService"
    factory-bean="serviceLocator"
    factory-method="createAccountServiceInstance"/>
           

The following example shows the corresponding class:

以下示例展示了相應的類:

Java

public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl();

    private static AccountService accountService = new AccountServiceImpl();

    public ClientService createClientServiceInstance() {
        return clientService;
    }

    public AccountService createAccountServiceInstance() {
        return accountService;
    }
}
           

Kotlin

class DefaultServiceLocator {
    companion object {
        private val clientService = ClientServiceImpl() 
        private val accountService = AccountServiceImpl() 
    }

    fun createClientServiceInstance() : ClientService {
        return clientService
    }

    fun createAccountServiceInstance() : AccountService {
        return accountService
    }
}
           

This approach shows that the factory bean itself can be managed and configured through dependency injection (DI). See Dependencies and Configuration in Detail.

這種方法表明,工廠bean本身可以通過依賴注入(DI)進行管理和配置。請檢視依賴項和配置詳情(Dependencies and Configuration in Detail)。

1.3.2.3. Instantiation by Using an Instance Factory Method (使用執行個體工廠方法進行執行個體化) Spring Framework Documentation (5.3.10)
In Spring documentation, "factory bean" refers to a bean that is configured in the Spring container and that creates objects through an instance or static factory method. By contrast, 

FactoryBean

 (notice the capitalization) refers to a Spring-specific FactoryBean implementation class.
1.3.2.3. Instantiation by Using an Instance Factory Method (使用執行個體工廠方法進行執行個體化) Spring Framework Documentation (5.3.10)
在Spring文檔中,“工廠bean”(factory bean)是指在Spring容器中配置的bean,它通過執行個體(instance )或靜态(static )工廠方法建立對象。相比之下,FactoryBean(注意大寫)指的是特定于Spring的FactoryBean實作類。

繼續閱讀