Rhyme/Spring bean的实例化 1.3.2. Instantiating beans

11 篇文章 0 订阅

Spring bean的实例化 1.3.2. Instantiating 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本质上是创建一个或多个对象的配方。当spring容器被询问创建对象时,它会查看对应命名的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 Instantiation using an instance factory method and Bean definition inheritance.) You use the Class property in one of two ways:

如果使用基于xml的配置元数据,则可以指定要在元素的class属性中实例化的对象的类型。这个class属性是一个内部BeanDefinition实例的classs属性,通常是强制的。(有关异常,请参阅使用工厂方法和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的构造方法来创建这个bean对象,有点类似于java使用new来创建对象。

  • 要指定具体的包含静态工厂方法用来创建一个对象的类,通过静态工厂方法来创建对象的情况很少见。从静态工厂方法返回的对象返回的对象类型可能是一个类或完全是不同的类。

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的定义中,你必须使用这个嵌套类的二进制名

例如。你有一个叫做Foo的类,在com.example.package包下,并且这个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 容器几乎可以管理任何你想被管理的类;它不限于管理真正的JavaBean.
大多数spring的用户喜欢实际的JavaBean,只要一个默认的空构造函数和在容器中的属性之后的适当的setter和getter方法。你可以在你容器中使用异国情调的非Java Bean风格的Bean.例如,你要使用绝对不符合JavaBean规范的传统连接池,spring也可以进行管理。

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

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

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

通过静态工厂方法来实例化

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将会使用工厂方法来创建。该定义没有定义返回的对象的类型,只有一个包含工厂方法的类。在这个例子中,creatIUnstance()方法必须是一个静态的方法

<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;
    }
}

使用工厂方法实例化

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-bean属性中填上包含实例化方法的在当前容器中的bean的name,这个bean的实例化方法将被用来创建指定的对象。使用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();

    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();

    public ClientService createClientServiceInstance() {
        return clientService;
    }

    public AccountService createAccountServiceInstance() {
        return accountService;
    }
}

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文档中,factory bean引用的是一个已经在spring容器中进行配置的bean,它将通过实例或静态工厂方法来创建。相比之下,FactoryBean(注意大小写)是指特定于spring的FactoryBean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值