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容器使用一种形式的配置元数据。此配置元数据表示您作为应用程序开发人员如何告诉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.
传统上,配置元数据使用简单直观的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的元数据并不是配置元数据的唯一允许的形式。Spring IoC容器本身是完全与配置元数据的实际编 写格式解耦的。现在,许多开发人员选择基于Java 的配置来开发他们的应用程序。 |
For information about using other forms of metadata with the Spring container, see:
-
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@DependsOnannotations.
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 <bean/> elements inside a top-level <beans/> element. Java configuration typically uses @Bean-annotated methods within a @Configuration class.
Spring配置由容器必须管理的至少一个(通常是一个以上)bean定义组成。基于XML的配置元数据将这些bean配置为<bean/>顶级元素内的<beans/>元素。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.
这些bean定义对应组成程序的实际对象。通常,你可以定义服务层对象,数据访问(DAOs)对象,表示层对象如Struts Action,基础对象如Hibernate SessionFactories,JMS 队列等等。通常不会在容器中配置细粒度的对象,因为这些对象的创建和加载是数据访问层和业务逻辑层的责任。然而,你可以使用spring集成AspectJ来配置那些ioc 容器控制之外所创建的对象。参考aop-configurable。
下面是一个基于xml的基本配置示例
The following example shows the basic structure of XML-based configuration metadata:
<?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
https://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 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的值。这里并没有给出示例,可以参考
浙公网安备 33010602011771号