基于java配置提供元数据_Spring-Framework-5.0.0.M3-CN

3.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.

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.

3017de66717157d32bd639a3800168a7.png

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.

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

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 @Configurationclass.

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.

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

如上图所示, Spring IoC容器使用一种形式的配置元数据;此配置元数据表示你作为应用程序开发人员如何告诉 Spring容器如何实例化,配置和组合应用程序中的对象。

配置元数据传统上以简单和直观的 XML格式提供,这是本章的大部分用来传达 Spring IoC容器的关键概念和特性。

c2cb9efd83937b5ef630e0e35d66543b.png

基于XML的元数据不是配置元数据唯一允许的形式。 Spring IoC容器本身完全与实际写入配置元数据的格式解耦。现在,许多开发人员为他们的Spring应用程序选择基于Java的配置。

有关在Spring容器中使用其他形式的元数据的信息,请参阅:

基于注释的配置: Spring 2.5引入了对基于注解的配置元数据的支持。

基于Java的配置:从 Spring 3.0开始,Spring JavaConfig项目提供的许多功能成为Spring Framework核心的一部分。因此,您可以使用Java而不是XML文件来定义应用程序类外部的bean。要使用这些新功能,请参阅 @Configuration, @Bean, @Importand @DependsOn注释。

Spring配置包括容器必须管理的至少一个,通常多个bean定义。基于XML的配置元数据显示这些bean配置为顶级元素内的元素。 Java配置通常在 @Configurationclass中使用 @Bean注解方法。

这些bean定义对应于组成应用程序的实际对象。通常,您定义服务层对象,数据访问对象(DAO),演示对象(如Struts Action实例),基础结构对象(如Hibernate SessionFactories),JMS队列等。通常, 不会在容器中配置细粒度域对象,因为通常由DAO和业务逻辑负责创建和加载域对象。但是,您可以使用 Spring与 AspectJ的集成来配置在 IoC容器控制之外创建的对象。请参阅使用 AspectJ通过 Spring来依赖注入域对象。

以下示例显示了基于 XML的配置元数据的基本结构:

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">

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的类型,并使用完全限定的类名。 id属性的值指协作对象(即容器内此bean的名称)。 在此示例中未显示用于引用协作对象的XML; 有关详细信息,请参阅依赖关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值