Rhyme/Spring 1.2.1. Configuration metadata学习笔记

来源:互联网 发布:effective java英文版 编辑:程序博客网 时间:2024/06/08 11:55

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:
基于注解和基于java的配置方式如下

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

基于注解的配置方式:开始于spring2.5

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的配置方式:自从spring 3.0开始,spring javaConfig项目提供的许多功能称为spring 框架的核心功能。因此你可以使用java的方式在你的应用中定义外部的bean而不是xml文件。使用这些功能,请看@Configuration, @Bean, @Import and @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.

spring 配置文件包含了多个至少一个必须被spring容器管理bean的定义。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.

这些bean的定义符合实际的用来组成你的应用的对象。典型的如你定义的service层的对象、数据访问层的对象dao,表示对象如struts的Action,基础设施对象如Hibernate的SessionFactories,JMS Queues(java消息服务队列)等等。通常我们并不会在容器中定义细粒度的域对象,因为它们通常是DAOs和业务逻辑的责任去创建和加载域对象。但是,你可以使用spring和AspectJ的集成来配置在spring ioc容器控制外的对象

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>    <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属性是一个bean的唯一标识,class属性定义了这个bean的java类的类型,并且使用全类名来表示。id属性可以用于合作者的引用,具体的请看上面的Depencies链接

阅读全文
0 0
原创粉丝点击