Spring 5.0.8.RELEASE文档 Core1-1.2.1 配置元数据

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格式(官网有smaple),本章节主要是转达Spring IoC容器的一些概念和特点。(提示:Spring不仅可以使用xml格式,越来越多开发者使用注解形式)

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 @DependsOn annotations.

关于怎么使用一组元数据去配置Spring容器,可以参看下面章节:
注解配置在1.9章节:Spring2.5开始支持注解配置。
基于Java代码的配置在1.12章节:Spring3.0开始支持,其中许多细节是由Spring JavaConfig Project提供的实现,已经成为了Spring框架的一部分。因此你可以声明beans在你的应用class外部通过使用Java而不是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配置文件通常包含一个或(通常)多个bean定义。xml配置文件配置的bean(使用包裹)必须被包括在父标签之下。Java类作为配置文件通常使用@Bean来注解方法,@Configuration来注解类的类作为配置类

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与你构建应用的实际类是一致的。通常你会用其(Spring来描述bean)来声明service类(服务类),dao类,对象类等,例如像 Struts Action实例、Hibernate SessionFactories、JMS Queues等等。通常不会在容器中去配置细粒度的域对象(领域模型驱动的实体类,也就是对象里面包含了一系列的业务方法)。但是你也可以通过利用Spring集成的AspectJ去配置(同一执行方法) 不在Ioc容器里面的对象。可以参考5.8.1。

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声明(id描述唯一bean)。class属性是来描述bean对应的关联的类,并且需要全路径。id的值可以用来指引合作类(依赖类、配置参数)。详细可以查看1.4章节。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值