版本5.3.16

This part of the reference documentation covers all the technologies that are absolutely integral to the Spring Framework.

参考文档的这一部分涵盖了所有与Spring框架绝对相关的技术。

Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container. A thorough treatment of the Spring Framework’s IoC container is closely followed by comprehensive coverage of Spring’s Aspect-Oriented Programming (AOP) technologies. The Spring Framework has its own AOP framework, which is conceptually easy to understand and which successfully addresses the 80% sweet spot of AOP requirements in Java enterprise programming.

其中最重要的是Spring框架的控制反转(IoC)容器。在对Spring框架的IoC容器彻底了解之后,将对Spring面向对象编程(AOP)技术进行全面介绍。Spring框架有自己的AOP框架,在概念上很容易理解,并且成功地解决了Java企业开发中80%的AOP需求点。

Coverage of Spring’s integration with AspectJ (currently the richest — in terms of features — and certainly most mature AOP implementation in the Java enterprise space) is also provided.

此外,还介绍了Spring与AspectJ(目前在Java企业领域功能最丰富、最成熟的AOP实现)的集成。

1. The IoC Container

This chapter covers Spring’s Inversion of Control (IoC) container.

本章介绍了Spring控制反转容器。

1.1. Introduction to the Spring IoC Container and Beans

Spring IOC和Bean简介

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern.

本章介绍了Spring框架中控制反转(IoC)的实现原理。IoC也被称为依赖注入(DI)。它是一个仅仅通过构造器的参数、工厂方法的参数、或者对象通过构造器实例化后或从工厂方法返回后设置其属性来定义对象依赖关系的过程。然后容器在创建bean的时候注入这些依赖关系。这个过程从根本上来说是Bean本身通过使用直接构造类或像服务定位模式的机制来控制其依赖关系的实例化或位置的逆过程(因此被称为控制反转)。

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object.

org.springframework.beans和org.springframework.context是Spring Framework IoC容器的基本包。BeanFactory接口提供了一种高级配置机制,它能够管理任何类型的对象。

ApplicationContext is a sub-interface of BeanFactory. It adds:

ApplicationContext是BeanFactory的一个子接口。该接口补充了以下内容:

① Easier integration with Spring’s AOP features;

更容易与Spring的AOP功能集成;

② Message resource handling (for use in internationalization);

消息资源处理(用于国际化);

③ Event publication;

事件发布;

④ Application-layer specific contexts such as the WebApplicationContext for use in web applications.

用于web应用程序的应用层特定上下文,如WebApplicationContext。

In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of Spring’s IoC container. For more information on using the BeanFactory instead of the ApplicationContext, see The BeanFactory.

简而言之,BeanFactory提供了配置框架和基本功能,而ApplicationContext则增加了更多的企业定制的功能。ApplicationContext是BeanFactory的完整超集,在本章中它专门用于Spring IoC容器的描述。而更多关于使用BeanFactory的信息可以跳转至The BeanFactory章节。

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

在Spring中,构成应用程序的主干并由Spring IoC容器管理的对象被称为Bean。Bean是一个由Spring IoC容器实例化、绑定和管理的对象。Bean以及他们之间的依赖关系反映在容器使用的配置元数据中。

1.2. Container Overview

容器概述

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects.

org.springframework.context.ApplicationContext接口代表Spring IoC容器,它负责所有bean的实例化、配置和绑定。容器通过读取配置元数据来获得关于要实例化、配置和绑定哪些对象的指示。配置元数据以XML、java注解或Java代码来表示。它可以让我们去创建和管理应用程序中的基本对象,以及他们之间丰富的相互依赖关系。

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. While XML has been the traditional format for defining configuration metadata, you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats.

Spring提供了几个ApplicationContext接口的实现。在独立的应用程序中,创建ClassPathXmlApplicationContext或FileSystemXmlApplicationContext的实例是很常见的。虽然XML是定义配置元数据的传统格式,但你也可以通提供少量的XML配置来指示容器去使用Java注解或Java代码作为元数据格式,以声明性地启用对这些额外元数据格式的支持。

In most application scenarios, explicit user code is not required to instantiate one or more instances of a Spring IoC container. For example, in a web application scenario, a simple eight (or so) lines of boilerplate web descriptor XML in the web.xml file of the application typically suffices (see Convenient ApplicationContext Instantiation for Web Applications). If you use the Spring Tools for Eclipse (an Eclipse-powered development environment), you can easily create this boilerplate configuration with a few mouse clicks or keystrokes.

在大多数应用场景中,不需要明确用户代码来实例化Spring IoC容器的一个或多个实例。例如,在一个web应用程序场景中,通常只需要再应用程序的web.xml文件中编写8行(或更多)模板式的web描述符就足够了(参见Convenient ApplicationContext Instantiation for Web Application)。如果你在Eclipse中使用Spring工具,你只需要点击几下鼠标或按键就可以可以轻松创建这种模板配置。

The following diagram shows a high-level view of how Spring works. Your application classes are combined with configuration metadata so that, after the ApplicationContext is created and initialized, you have a fully configured and executable system or application.

下图显示了Spring如何工作的高级视图。首先你的应用类与配置元数据相结合,然后创建和初始化ApplicationContext,你就有了一个完整配置的可执行系统或应用程序。

image.png

1.2.1. Configuration Metadata

配置元数据

As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.

如上图所示,Spring IoC容器使用配置metadata的形式。这个配置metadata表示作为应用程序开发人员,如果告诉Spring容器实例化、配置和绑定应用程序的对象。

Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is what most of this chapter uses to convey key concepts and features of the Spring IoC container.

传统上,配置metadata是通过一个简单直观的XML文件进行的,本章主要讲述Spring IoC容器的关键概念和特性。

XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written. These days, many developers choose Java-based configuration for their Spring applications.

基于XML的metadata并不是配置metadata的唯一方式。Spring IoC容器本身与这种配置metadata的实际编写格式是解耦的。如今,一些开发人员在他们的Sprin应用程序中选择基于Java的配置。

For information about using other forms of metadata with the Spring container, see:

关于Spring容器中使用其他形式的metadata的信息,如下:

  • Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.

    基于注解的配置:Spring2.5引入了基于注解配置metadata。

  • Java-based configuration: Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the @Configuration, @Bean, @Import, and @DependsOn annotations.

    基于Java代码的配置:从spring3.0开始,Spring javaconfig项目提供的很多功能称为Spring Framework的核心部分。因此,你可以使用Java而不是XML文件来定义应用类外部的Bean。如果要使用这些新的功能,可以参考@Configuration, @Bean, @Import和@DependsOn注解。

Spring configuration consists of at least one and typically more than one bean definition that the container must manage. XML-based configuration metadata configures these beans as elements inside a top-level element. Java configuration typically uses @Bean-annotated methods within a @Configuration class.

Spring配置包括至少一个(通常是一个以上)Bean的定义,容器必须管理这些Bean。基于XML的配置metadata是使用标签元素来配置这些bean。通常Java配置是在一个有注解@Configuration 的类中使用带有注解@Bean-annotated的方法来实现。

These bean definitions correspond to the actual objects that make up your application. Typically, you define service layer objects, data access objects (DAOs), presentation objects such as Struts Action instances, infrastructure objects such as Hibernate SessionFactories, JMS Queues, and so forth. Typically, one does not configure fine-grained domain objects in the container, because it is usually the responsibility of DAOs and business logic to create and load domain objects. However, you can use Spring’s integration with AspectJ to configure objects that have been created outside the control of an IoC container. See Using AspectJ to dependency-inject domain objects with Spring.

这些bean的定义相当于是构成你的应用程序的实际对象。通常,你可以定义服务层对象、数据访问层对象、演示对象(如Strusts Action实例)、基础设施对象(例如Hibernate SessionFatories、JMS队列)等等。通常情况下,人们不会在容器中配置细粒度的域对象,因为创建和加载域对象通常是DAO和业务逻辑的责任。然而,你可使用Spring和AspectJ的集成来配置那些在IoC容器控制之外创建的对象。参加使用AspectJ与Spring进行依赖注入领域域对象章节内容。

The following example shows the basic structure of XML-based configuration metadata:

下面的例子是基于XML配置metadata的基本结构:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="..." class="...">
  7. <!-- collaborators and configuration for this bean go here -->
  8. </bean>
  9. <bean id="..." class="...">
  10. <!-- collaborators and configuration for this bean go here -->
  11. </bean>
  12. <!-- more bean definitions go here -->
  13. </beans>

① The id attribute is a string that identifies the individual bean definition.

id属性是一个字符串,它用于识别一个bean的定义。

② The class attribute defines the type of the bean and uses the fully qualified
classname.

class属性定义了bean的类型,并要使用完整的全类名。

The value of the id attribute refers to collaborating objects. The XML for referring to collaborating objects is not shown in this example. See Dependencies for more information.

id属性的值指向了绑定的对象。本例中没有显示引用绑定对象的XML。更多信息请参见依赖关系章节内容。

1.2.2. Instantiating a Container 实例化一个容器

The location path or paths supplied to an ApplicationContext constructor are resource strings that let the container load configuration metadata from a variety of external resources, such as the local file system, the Java CLASSPATH, and so on.

提供给ApplicationContext构造函数的一条或多条路径是资源字符串,它让容器从各种外部资源(如本地文件系统、Java的classpath等)中加载配置metadata。

  1. ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
After you learn about Spring’s IoC container, you may want to know more about Spring’s Resource abstraction (as described in Resources), which provides a convenient mechanism for reading an InputStream from locations defined in a URI syntax. In particular, Resource paths are used to construct applications contexts, as described in Application Contexts and Resource Paths.

在了解了Spring IoC容器之后,你可能想了解更多关于Spring的资源抽象(如Resources中所述),它为从URI语法中定义的位置读取inputstream提供了方便的机制。特别是资源路径被用来构建应用的上下文,如在“Application Contexts and Resource Paths”章节所描述。

The following example shows the service layer objects (services.xml) configuration file:

下面例子显示的是服务层对象(services.xml)的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!-- services -->
  7. <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
  8. <property name="accountDao" ref="accountDao"/>
  9. <property name="itemDao" ref="itemDao"/>
  10. <!-- additional collaborators and configuration for this bean go here -->
  11. </bean>
  12. <!-- more bean definitions for services go here -->
  13. </beans>

The following example shows the data access objects daos.xml file:

下面例子显示的是数据访问层对象daos.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="accountDao"
  7. class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
  8. <!-- additional collaborators and configuration for this bean go here -->
  9. </bean>
  10. <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
  11. <!-- additional collaborators and configuration for this bean go here -->
  12. </bean>
  13. <!-- more bean definitions for data access objects go here -->
  14. </beans>

In the preceding example, the service layer consists of the PetStoreServiceImpl class and two data access objects of the types JpaAccountDao and JpaItemDao (based on the JPA Object-Relational Mapping standard). The property name element refers to the name of the JavaBean property, and the ref element refers to the name of another bean definition. This linkage between id and ref elements expresses the dependency between collaborating objects. For details of configuring an object’s dependencies, see Dependencies.

在上面的例子中,服务层是由PetStoreServiceImpl类和两个类型为JpaAccountDao和JpaItemDao的数据访问层对象组成。(基于JPA对象-关系映射标准)。其中,property标签中的name元素名称是Javabean属性的名称,ref元素指向的是另一个定义bean的名称。id和ref属性之间的关系表示了对象之间的依赖关系。关于配置一个对象的依赖关系的详细内容,可查阅“Dependencies”章节。

Composing XML-based Configuration Metadata

构建基于XML的配置Metadata

It can be useful to have bean definitions span multiple XML files. Often, each individual XML configuration file represents a logical layer or module in your architecture.

让bean的定义跨越多个XML文件是很有用的。通常情况下,每个单独的XML配置文件代表架构中的一个逻辑层或者一个模块。

You can use the application context constructor to load bean definitions from all these XML fragments. This constructor takes multiple Resource locations, as was shown in the previous section. Alternatively, use one or more occurrences of the element to load bean definitions from another file or files. The following example shows how to do so:

你可以使用application context构造函数从所有的XML文件中加载bean的定义。这个构造函数多个资源locations,如上一节所示。或者,使用一个或者多个元素从另一个或多个文件中加载bean的定义。下面的例子显示了如何做到这一点:

  1. <beans>
  2. <import resource="services.xml"/>
  3. <import resource="resources/messageSource.xml"/>
  4. <import resource="/resources/themeSource.xml"/>
  5. <bean id="bean1" class="..."/>
  6. <bean id="bean2" class="..."/>
  7. </beans>

In the preceding example, external bean definitions are loaded from three files: services.xml, messageSource.xml, and themeSource.xml. All location paths are relative to the definition file doing the importing, so services.xml must be in the same directory or classpath location as the file doing the importing, while messageSource.xml and themeSource.xml must be in a resources location below the location of the importing file. As you can see, a leading slash is ignored. However, given that these paths are relative, it is better form not to use the slash at all. The contents of the files being imported, including the top level element, must be valid XML bean definitions, according to the Spring Schema.

在前面的例子中,外部的Bean定义从以下三个文件汇总被加载,这个三个文件分别是services.xml、messageSource.xml和themeSorurce.xml。所有的位置都是相对于进行导入的定义文件而言的,所以service.xml必须与进行导入的文件在同一目录,或者是classpath和其路径一致,而messageSource.xml和themeSource.xml必须在导入文件下的resource目录里。正如你所看到的,前导斜线会被忽略。然而,鉴于这些路径都是相对的,最好不要使用前导斜线。根据Spring模式,被导入的文件内容(包括顶层的元素)必须是有效的XML bean定义。

| It is possible, but not recommended, to reference files in parent directories using a relative “../“ path. Doing so creates a dependency on a file that is outside the current application. In particular, this reference is not recommended for classpath: URLs (for example, classpath:../services.xml), where the runtime resolution process chooses the “nearest” classpath root and then looks into its parent directory. Classpath configuration changes may lead to the choice of a different, incorrect directory.> 使用相对路径“../”来引用父目录的文件也是可以的,但是不推荐这样做。这样做会造成对当前应用程序之外的文件的依赖。特别是不建议使用在classpath:URLs中(例如:classpath:../services.xml),它会在运行的过程中选择“nearest”classpath root,然后查找其父目录。classpath配置的改变可能会导致选择不同的或者不正确的目录。

You can always use fully qualified resource locations instead of relative paths: for example, file:C:/config/services.xml or classpath:/config/services.xml. However, be aware that you are coupling your application’s configuration to specific absolute locations. It is generally preferable to keep an indirection for such absolute locations — for example, through “${…}” placeholders that are resolved against JVM system properties at runtime.> 你可以使用资源位置的绝对路径来替代相对路径。例如:file:C:/config/services.xml 或者 classpath:/config/services.xml。请注意,你正在将应用程序应用程序的配置与特定的绝对位置耦合。一般来说,最好是为这种绝对路径保留一个标识,比如在运行时通过“${…}”占位符来解析指定的JVM系统属性。

| | —- |

The namespace itself provides the import directive feature. Further configuration features beyond plain bean definitions are available in a selection of XML namespaces provided by Spring — for example, the context and util namespaces.

命名空间本身提供了导入指令的功能。除了简单的bean定义之外,更多的配置功能都可以在spring提供的XML命名空间中获得,例如context和util命名空间。

The Groovy Bean Definition DSL

Groovy Bean定义DSL

As a further example for externalized configuration metadata, bean definitions can also be expressed in Spring’s Groovy Bean Definition DSL, as known from the Grails framework. Typically, such configuration live in a “.groovy” file with the structure shown in the following example:

作为外部化配置metadata的另外一个例子,bean定义也可以用Spring的Groovy Bean Dfinition DSL来表达,正如Grails框架那样。通常情况下,这种配置存在于“.groovy”文件中,其结构如下所示:

  1. beans {
  2. dataSource(BasicDataSource) {
  3. driverClassName = "org.hsqldb.jdbcDriver"
  4. url = "jdbc:hsqldb:mem:grailsDB"
  5. username = "sa"
  6. password = ""
  7. settings = [mynew:"setting"]
  8. }
  9. sessionFactory(SessionFactory) {
  10. dataSource = dataSource
  11. }
  12. myService(MyService) {
  13. nestedBean = { AnotherBean bean ->
  14. dataSource = dataSource
  15. }
  16. }
  17. }

This configuration style is largely equivalent to XML bean definitions and even supports Spring’s XML configuration namespaces. It also allows for importing XML bean definition files through an importBeans directive.

这种配置风格在很大程度上等同于XML Bean的定义,甚至支持Spring的XML配置命名空间。同时,它也可以通过importBeans指令来导入XML bean定义文件。

1.2.3. Using the Container

使用容器

The ApplicationContext is the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. By using the method T getBean(String name, Class requiredType), you can retrieve instances of your beans.

ApplicationContext是一个高级工厂接口,它能够管理不同Bean的注册以及他们之间的依赖关系。通过使用方法 T getBean(String name, Class requiredType),可以获取到bean的实例。

The ApplicationContext lets you read bean definitions and access them, as the following example shows:

ApplicationContext可以获取bean的定义并访问它们,如下例所示:

  1. // create and configure beans
  2. ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
  3. // retrieve configured instance
  4. PetStoreService service = context.getBean("petStore", PetStoreService.class);
  5. // use configured instance
  6. List<String> userList = service.getUsernameList();

With Groovy configuration, bootstrapping looks very similar. It has a different context implementation class which is Groovy-aware (but also understands XML bean definitions). The following example shows Groovy configuration:

通过Groovy配置,引导看起来非常相似。它有一个不同的上下文实现类是Groovy-aware(但也能理解XML bean的定义)。下面例子显示了Groovy的配置:

  1. ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", "daos.groovy");

The most flexible variant is GenericApplicationContext in combination with reader delegates — for example, with XmlBeanDefinitionReader for XML files, as the following example shows:

最灵活的区别是GenericApplicationContext与reader delegates相结合。例如,它与XmlBeanDefinitionReader一起作用于XML文件,如下例所示:

  1. GenericApplicationContext context = new GenericApplicationContext();
  2. new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
  3. context.refresh();

You can also use the GroovyBeanDefinitionReader for Groovy files, as the following example shows:

也可以将GroovyBeanDefinitionReader用于Groovy文件,如下例所示:

  1. GenericApplicationContext context = new GenericApplicationContext();
  2. new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");
  3. context.refresh();

You can mix and match such reader delegates on the same ApplicationContext, reading bean definitions from diverse configuration sources.

可以在同一个ApplicationContext中混合使用,并且匹配这些reader delegates,从不同的配置源中获取bean的定义。

You can then use getBean to retrieve instances of your beans. The ApplicationContext interface has a few other methods for retrieving beans, but, ideally, your application code should never use them. Indeed, your application code should have no calls to the getBean() method at all and thus have no dependency on Spring APIs at all. For example, Spring’s integration with web frameworks provides dependency injection for various web framework components such as controllers and JSF-managed beans, letting you declare a dependency on a specific bean through metadata (such as an autowiring annotation).

可以使用getBean方法来获取bean的实例。ApplicationContext接口还有一些其他的方法来获取bean,但理想情况下,应用程序代码不应该使用这些方法。事实上,你的应用程序代码根本不应该调用getBean()方法,因此对Spring的API根本就没有依赖性。例如,Spring与web框架的集成为各种web框架组件(如控制器和JSF-managed bean)提供了依赖注入,让你通过metadata(例如autowiring注解)来声明对特定bean的依赖。

1.3. Bean Overview

Bean概述

A Spring IoC container manages one or more beans. These beans are created with the configuration metadata that you supply to the container (for example, in the form of XML definitions).

Spring IoC容器管理着一个或多个bean。这些bean是通过提供给容器的配置metadata创建的。(例如:以XML 定义的形式)

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:

在容器汇总,这些bean定义作为BeanDefinition对象被表示,它包含以下metadata(除其他信息外):

  • A package-qualified class name: typically, the actual implementation class of the bean being defined.

    一个封装合格的类名:通常,是一个被定义bean的实现类。

  • Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).

    bean的行为配置元素,说明bean在容器中的行为方式(范围、生命周期回调等)

  • References to other beans that are needed for the bean to do its work. These references are also called collaborators or dependencies.

    这些bean在运行时需要引用其他bean,这些引用被称为依赖关系。

  • Other configuration settings to set in the newly created object — for example, the size limit of the pool or the number of connections to use in a bean that manages a connection pool.

    在新创建对象时配置其他设置,比如在管理连接池中的bean时可以设置连接池的大小或者连接的数量。

This metadata translates to a set of properties that make up each bean definition. The following table describes these properties:

这个metadata转化为构成bean定义的一系列属性。下面表中描述了这些属性:

Property Explained in…
Class Instantiating Beans 实例化bean
Name Naming Beans bean的名称
Scope Bean Scopes bean的范围
Constructor arguments Dependency Injection 依赖注入
Properties Dependency Injection 依赖注入
Autowiring mode Autowiring Collaborators 自动协作
Lazy initialization mode Lazy-initialized Beans 实例化bean
Initialization method Initialization Callbacks 初始化回调
Destruction method Destruction Callbacks 销毁回调

In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container (by users). This is done by accessing the ApplicationContext’s BeanFactory through the getBeanFactory() method, which returns the BeanFactory DefaultListableBeanFactory implementation. DefaultListableBeanFactory supports this registration through the registerSingleton(..) and registerBeanDefinition(..) methods. However, typical applications work solely with beans defined through regular bean definition metadata.

除了包含如何创建特定bean信息的bean定义外,ApplicationContext的实现允许在容器外部(由用户)创建的现有对象的注册。这是通过getBeanFactory()方法访问ApplicationContext的BeanFactory()来实现,该方法返回BeanFactory DefaultListableBeanFactory实现。DefaultListableBeanFacotry支持通过方法registerSingleton()和registerBeanDefinition()来进行注册。然而,典型的应用程序只能通过固定的bean定义metadata来定义bean。