Instantiating beans

3.3.2 Instantiating beans (实例化beans)

A bean definition essentially is a recipe for creating one or more objects. The container looks at the recipe for a named bean when asked, and uses the configuration metadata encapsulated by that bean definition to create (or acquire) an actual object.


bean的定义本质上是 创建一个多个对象的配方。当需要的时候,容器会在该配方中查找这个bean,并根据 配置数据(由bean对象组成)来创建实体对象。


If you use XML-based configuration metadata, you specify the type (or class) of object that is to be instantiated in the class attribute of the element. This class attribute, which internally is a Class property on a BeanDefinition instance, is usually mandatory. (For exceptions, see the section called “Instantiation using an instance factory method” and Section 3.7, “Bean definition inheritance”.) You use the Class property in one of two ways:


如果你使用基于XML 的配置元数据,<bean> 节点中 class 的属性会是你实例化的对象的类型或者类, class属性在 内部表示一个BeanDefinition 实例是必须要要的。(由问题,请查看”Instantiation using an instance factory method“和 第3.7 张 ”bean 定义集成“),class 通常由两种使用形式。


  • Typically, to specify the bean class to be constructed in the case where the container itself directly creates the bean by calling its constructor reflectively, somewhat equivalent to Java code using the new operator.

  • To specify the actual class containing the static factory method that will be invoked to create the object, in the less common case where the container invokes a static factory method on a class to create the bean. The object type returned from the invocation of the static factory method may be the same class or another class entirely.


  • 通常,在容器本身通过调用反射构造器方式创建创建bean,某些情况下与Java 代码里使用new 创建实例一样。
  • 要指定包含静态工厂方式的实体类用来创建对象,在极少数情况下,容器调用静态工厂方法,来创建bean,从 静态工厂 返回的 对象类型,可能是相同的或者完全的另一个类。

Inner class names. If you want to configure a bean definition for a static nested class, you have to use the binary name of the nested class.

For example, if you have a class called Foo in the com.example package, and this Foo class has a static nested class called Bar, the value of the ‘class’ attribute on a bean definition would be…​

com.example.Foo$Bar

Notice the use of the $ character in the name to separate the nested class name from the outer class name.


内部类名,如果你要为静态嵌套类配置 bean定义,你必须使用,嵌套类的二进制名称。
举个例子,你在 com.example 下有一个类 Foo 这个Foo 类有个 静态嵌套类 叫做Bar,则bean 定义上class 将会是com.example.Foo$Bar
注意使用$ 将外部类以及嵌套静态类分开。

Instantiation with a constructor(使用构造器进行实例化)

When you create a bean by the constructor approach, all normal classes are usable by and compatible with Spring. That is, the class being developed does not need to implement any specific interfaces or to be coded in a specific fashion. Simply specifying the bean class should suffice. However, depending on what type of IoC you use for that specific bean, you may need a default (empty) constructor.


当你使用构造方法来创建 bean是,所有的普通类都可以使用并且与Spring 兼容,也就是说,正在开发的类不需要实现任何特定的接口或者特定格式的编码,简单的指定bean 类就够了,然而,根据你指定的bean要使用的IoC 的类型,你可能需要一个默认的(空的)构造函数。


The Spring IoC container can manage virtually any class you want it to manage; it is not limited to managing true JavaBeans. Most Spring users prefer actual JavaBeans with only a default (no-argument) constructor and appropriate setters and getters modeled after the properties in the container. You can also have more exotic non-bean-style classes in your container. If, for example, you need to use a legacy connection pool that absolutely does not adhere to the JavaBean specification, Spring can manage it as well.


Spring IoC容易可以管理几乎你需要管理的所有类型的 类,没有限制去管理真的JavaBeans。多数Spring 用户喜欢容器中有默认(无参)构造器以及除此外适当的getters以及setter方法。容器中还可以有更多外部的 non-bean-syle 的类,例如:如果你需要一个完全不遵守JavaBean规范的 旧版的链接池,Spring 也可以管理它。


With XML-based configuration metadata you can specify your bean class as follows:

使用基于XML 配置元数据,你可以如下方式指定你的bean 类

<bean id="exampleBean" class="examples.ExampleBean"/>

<bean name="anotherExample" class="examples.ExampleBeanTwo"/>

For details about the mechanism for supplying arguments to the constructor (if required) and setting object instance properties after the object is constructed, see Injecting Dependencies.


了解关于 向构造器中提供参数,并在构造完对象后向其中添加对象实例的机制,查看 依赖注入(Injecting Dependencies.)。

Instantiation with a static factory method(使用静态方式实例化)

When defining a bean that you create with a static factory method, you use the class attribute to specify the class containing the static factory method and an attribute named factory-method to specify the name of the factory method itself. You should be able to call this method (with optional arguments as described later) and return a live object, which subsequently is treated as if it had been created through a constructor. One use for such a bean definition is to call static factories in legacy code.


当你定义使用静态工厂方法创建的bean时,使用class 属性来指定包含静态工厂的类,或者factory-method来指定,工厂方法本身,你可以调用这个方法来返回一个动态的对象(使用下下文中将提到的可选参数)。虽然这个对象将会被看成通过构造器构造的。这种定义bean的一个用途就是调用旧版本中的静态工厂。


The following bean definition specifies that the bean will be created by calling a factory-method. The definition does not specify the type (class) of the returned object, only the class containing the factory method. In this example, the createInstance() method must be a static method.


下面的bean定义指定的bean将会使用工厂模式来创建,没有定义返回对象的类型(类),仅仅是包括工厂方法的类,,例子中createInstance()必须是个静态方法。

<bean id="clientService"
    class="examples.ClientService"
    factory-method="createInstance"/>
public class ClientService {
    private static ClientService clientService = new ClientService();
    private ClientService() {}

    public static ClientService createInstance() {
        return 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.

如果想了解向工厂方法提供参数的机理或在构造之后设置object,详见依赖和配置。

Instantiation 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/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.


与使用静态工厂方法类似,从实例工厂方法中,调用了容易中存在的bean的非静态方法 来创建新的bean。使用这个机制,需要将class 属性设置为空。 factory-bea属性中 指定 当前(或者 父容器)容器中创建对象 需要调用的方法所在的bean的name。factory-method的属性为创建对象的方法,

<!-- 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"/>
public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl();
    private DefaultServiceLocator() {}

    public ClientService createClientServiceInstance() {
        return clientService;
    }
}

One factory class can also hold more than one factory method as shown here:


一个工厂类中可以包含多个工厂方法。

<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"/>
public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl();
    private static AccountService accountService = new AccountServiceImpl();

    private DefaultServiceLocator() {}

    public ClientService createClientServiceInstance() {
        return clientService;
    }

    public AccountService createAccountServiceInstance() {
        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(依赖注入)来进行管理以及配置。

In Spring documentation, factory bean refers to a bean that is configured in the Spring container that will create objects through an instance or static factory method. By contrast, FactoryBean (notice the capitalization) refers to a Spring-specific FactoryBean.


在Spring 文中, 工厂bean 指的是Spring 容器中配置的bean通过实例或者静态工厂方法创建对象。相反 FactoryBean (注意大小写)指的就是 Springle 特定的 FactoryBean。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值