Spring 5.0.8.RELEASE文档 Core1-1.3.2 实例化 beans

版权声明:mj1001001 https://blog.csdn.net/qq_42786889/article/details/81742273

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的实际对象(参数已经设置好)

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属性是强制要赋值的,可以查看1.7和1.3.2章节。你可以使用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.

通常,指定bean的class是为了容器能通过反射调用构造函数从而正确地创建它们,有点等价于Java代码的new操作。(性能、语言层面上安全性不一样!反射就是破坏语言层面上的安全性)

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包含静态工厂方法(构造函数可能会是private),如果这个bean需要用静态工厂方法创建,Spring也可以支持,通过配置factory-method,这时要注意静态工厂返回的bean实例可能是其他class类型的。

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.

内部类names
如果你想要为静态内部类配置bean,你必须使用内部类的二进制名称。

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

例如,如果你有一个class叫Foo,在 com.example package 包下,并且Foo.class有一个静态内部类叫Bar,如果你想要定义Bar的bean,这个bean的class就应该是com.example.Foo$Bar。

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

当你命名内部类时记得使用$符号去分割外部类和内部类。

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,所有正常的class Spring都能兼容。这些正常能用构造函数初始化的bean直接使用class属性指定就足够了。无论如何,使用IoC容器去创建bean,你都需要为bean写上一个默认构造函数(如果构造函数带参数还需要定义一个不带参数的构造函数)

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容器能管理任何class;它不仅限于管理JavaBeans。许多Spring使用者更倾向于使用JavaBean(默认构造函数,所有属性有getter和setter)。你也可以用其来管理不属于JavaBean范式的class,例如线程池(没有get/set吧,可能是通过工厂模式创建的)这种,Spring也能管理。

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

你可以这样指定bean class在xml配置文件下:

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

更多详细机制例如支持带参数的构造函数实例化和实例化后设置属性,可以查看1.4.1章节。

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实例的,下面返回的对象是没有指定class(Object),仅仅是配置了工厂class和对应的工厂创建方法。下面的例子中,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.

有关向工厂方法提供(可选的)参数的机制,以及从工厂返回对象后设置对象实例属性的详细信息,请参阅1.4.2章节。

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属性留空,在工厂bean属性中,指定当前(或父/祖先)容器中的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"/>
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;
    }
}

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

这种途径显示工厂bean可以通过依赖注入来配置和管理。详细可查看1.4.2章节。

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 (注意大小写)是指Spring中的 FactoryBean。(创建Spring容器的工厂)

猜你喜欢

转载自blog.csdn.net/qq_42786889/article/details/81742273