onfiguration metadata

来源:互联网 发布:化学动画软件 编辑:程序博客网 时间:2024/06/03 21:44

3.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容器处理某一种格式的元数据, 这些配置数据代表了作为程序开发者想要告诉IOC容器 实例化,配置和装配那些对象。


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.


配置元数据可是是简单明了的XML 格式文件,我们本章节也使用的多是也是这个用来传递Spring IOC 容易的关键信息以及特征。


[Note]
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 并不是唯一的元数据格式,先在Spring Ioc与配置元数据 已经完全解耦了,最近许多开发者在开发Spring 应用的时候选择使用JAVA 配置


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


有关Spring Ioc元数据的其他格式,请参阅
- Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration 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.


  • 基于注解配置:Spring2.5 开始支持基于注解形式的元数据配置。
  • 基于Java配置:Spring JavaConfig 项目的很多功能被加入到了Spring Framework 的核心包,因此你可以使用JAV 而不是XML 文件来定义你的应用程序的扩展beans,要使用这些新的功能,查看@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 shows these beans configured as elements inside a top-level element. Java configuration typically uses @Bean annotated methods within a @Configuration class.


Springl 配置容器通常会管理一个或多个beans,基于XML的元数据会将这些配置包裹在 <beans>节点下的<bean>中,Java 注解配置 方式 通常使用在@configuration 注解的类中中使用@bean 注解某一个方法。


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.


这些baen的定义需要对应构成你app的实际对象,通常,你定义service 层对象, 数据接入层对象(DAO),显示层对象 例如Structs Action 实例,基础对象 例如 Hibernate SessionFactories 以及 JMS Queues。通常在 容器中不会配置 细致粒度 域对象,因为它通常用来负责进行 DAOs 和 业务逻辑对象的创建以及 域对象的加载。你可以使用Spring集成AspectJ配置,在Ioc容器的控制之外的beans。 查看 使用Spring 使用Aspectj进行域对象的依赖注入。


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

— 下面的例子展示了基于XML 配置数据结构。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="..." class="...">        <!-- collaborators and configuration for this bean go here -->    </bean>    <bean id="..." class="...">        <!-- collaborators and configuration for this bean go here -->    </bean>    <!-- more bean definitions go here --></beans>

The id attribute is a string that you use to identify the individual bean definition. The class attribute defines the type of the bean and uses the fully qualified classname. 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 的属性是String 你用来唯一标识一个bean。Class的属性用类的全限定类型,定义bean的类型。这个示例中没有包含的value 属性,用来定义类包含的对象。更多信息,查看依赖关系。