Spring Framework学习之路(3)---配置元数据

2.1. Configuration metadata

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格式提供,这是本章大部分内容用来传达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容器本身与实际写入此配置元数据的格式完全脱钩。 如今,许多开发人员为他们的Spring应用程序选择基于Java的配置。
  For information about using other forms of metadata with the Spring container, see:
  有关在Spring容器中使用其他形式的元数据的信息,请参阅:

  • Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.
  • 基于注解的配置:Spring 2.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而不是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 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配置通常在@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定义对应于组成应用程序的实际对象。通常,您定义服务层对象,数据访问对象(DAO),表示对象(例如Struts Action实例),基础结构对象(例如Hibernate SessionFactories,JMS队列)等等。通常,不会在容器中配置细粒度的域对象,因为创建和加载域对象通常是DAO和业务逻辑的职责。但是,您可以使用Spring与AspectJ的集成来配置在IoC容器控制之外创建的对象。请参阅使用AspectJ与Spring依赖注入域对象。

  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="...">1、2        
 		<!-- 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>

1、The id attribute is a string that identifies the individual bean definition.
1、id属性是一个标识单个bean定义的字符串。
2、The class attribute defines the type of the bean and uses the fully qualified classname.
2、class属性定义bean的类型并使用完全限定的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属性的值是指协作对象。 本示例中未显示用于引用协作对象的XML; 有关更多信息,请参阅依赖关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工地码哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值