天天看点

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实现类。

继续阅读