core手册翻译day13

The following example shows a class that would work with the preceding bean definition:
下列的例子展示了一个与前面类定义一起"工作"的类:

Java写法

public class ClientService {
   private static ClientService clientService = new ClientService();
   private ClientService() {}
   public static ClientService createInstance() {
   return clientService;
   }
}

Kotlin写法

class ClientService private constructor() {
   companion object {
   private val clientService = ClientService()
   fun createInstance() = clientService
   }
}

For details about the mechanism for supplying (optional) arguments to the factory method and setting object instance properties after the object is returned from the factory, see Dependencies and Configuration in Detail.
关于对工厂方法提供可选参数和设置在工厂返回对象的实例属性的详细机制,请仔细查阅依赖和配置这张。

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.
与静态工厂方法实例化相类似,实例工厂方法实例化是从一个在容器中的bean调用一个非静态方法来创造一个新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.
为了使用这个机制,请让类属性空着,以及在factiry-bean属性中,指定一个bean的名字,其存在当前(或父亲或祖先)容器且容器包含可供调用创造该对象的实例方法。

Set the name of the factory method itself with the factory-method attribute.
请看工厂方法名字本身和工厂方法属性。

The following example shows how to configure such a bean:
下列的例子展示了如何去配置这样的一个bean:

<!-- the factory bean, which contains a method called createInstance() -->
<!-- 工厂bean,它包含了一个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通过工厂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).
这种处理方法标识工厂bean本身也可以通过依赖注入方法被管理和配置。

See Dependencies and Configuration in Detail.
详情请看依赖和配置。

注意

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.
在Spring文档中,“工厂bean”指的是一个被Sping容器管理的bean兵器它通过一个实例或静态工厂方法创造bean。

By contrast, FactoryBean (notice the capitalization) refers to a Spring-specific FactoryBean implementation class.
相比之下,FactoryBean(注意大写)指的是一个Spring容器具体的FactoryBean实现类。

Determining a Bean’s Runtime Type

确定Bean运行时的类型。

The runtime type of a specific bean is non-trivial to determine.
一个具体的bean运行时的类型并不容易确定。

A specified class in the bean metadata definition is just an initial class reference, potentially combined with a declared factory method or being a FactoryBean class which may lead to a different runtime type of the bean, or not being set at all in case of an instance-level factory method (which is resolved via the specified
factory-bean name instead).
一个在bean元数据中定义的具体类只是一个初始类的引用,其可能与一个已声明的工厂方法或者一个可能导致bean有不同运行类型的工厂类,或者一个没有设置所有情况的实例级工厂方法(该方法通过具体的factory-bean命名而代替)。

Additionally, AOP proxying may wrap a bean instance with an interface-based proxy with limited exposure of the target bean’s actual type (just its implemented
interfaces).
此外,AOP代理可能会使用一个基于接口的代理包裹一个bean实例有限地暴露了目标bean的实际类型。(仅限其实现的接口)。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容