1.2.2.1 Composing XML-based Configuration Metadata组合基于XML的配置元数据

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

 Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.2.1. Configuration Metadata (配置元数据)

1.2.2. Instantiating a Container(实例化一个容器)

1.2.2.1 Composing XML-based Configuration Metadata(组合基于XML的配置元数据)

1.2.2.2 The Groovy Bean Definition DSL (Groovy Bean定义DSL)

1.2.3. Using the Container (使用容器)

1.3. Bean Overview (Bean概览)


下载此文档精编完整版

 No.内容下载地址文档内容目录
1中英双语精编版 第一部分PDF下载内容目录
2中英双语精编版 第二部分PDF下载内容目录
3中文精编版 第一部分PDF下载内容目录
4中文精编版 第二部分PDF下载内容目录

更多章节内容,请点击查看:  Core Technologies


1.2.2.1 Composing XML-based Configuration Metadata组合基于XML的配置元数据

It can be useful to have bean definitions span multiple XML files. Often, each individual XML configuration file represents a logical layer or module in your architecture.

bean定义跨越多个XML文件可能很有用。通常,每个单独的XML配置文件都表示你的体系结构中的一个逻辑层或模块。

You can use the application context constructor to load bean definitions from all these XML fragments. This constructor takes multiple Resource locations, as was shown in the previous section. Alternatively, use one or more occurrences of the <import/> element to load bean definitions from another file or files. The following example shows how to do so:

您可以使用应用程序上下文(application context)构造函数通过这些XML片段来加载bean定义。此构造函数获取多个Resource 位置,如前一节所示。或者,使用一个或多个< import />元素从另一个(或多个)文件加载bean定义。以下示例展示了如何执行此操作:

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

In the preceding example, external bean definitions are loaded from three files: services.xmlmessageSource.xml, and themeSource.xml. All location paths are relative to the definition file doing the importing, so services.xml must be in the same directory or classpath location as the file doing the importing, while messageSource.xml and themeSource.xml must be in a resources location below the location of the importing file. As you can see, a leading slash is ignored. However, given that these paths are relative, it is better form not to use the slash at all. The contents of the files being imported, including the top level <beans/> element, must be valid XML bean definitions, according to the Spring Schema.

在前述示例中,从三个文件加载外部bean定义:services.xmlmessageSource.xmlthemeSource.xml。所有位置路径都与执行导入的定义文件相关,因此services.xml必须与执行导入的文件位于同一目录或类路径(classpath)位置,而messageSource.xmlthemeSource.xml必须位于导入文件位置下的resources 位置。如您所见,前导斜杠被忽略。然而,考虑到这些路径是相对的,最好不要使用斜杠。根据Spring Schema,要导入的文件的内容,包括顶级<beans/>元素,必须是有效的XML bean定义。

It is possible, but not recommended, to reference files in parent directories using a relative "../" path. Doing so creates a dependency on a file that is outside the current application. In particular, this reference is not recommended for classpath: URLs (for example, classpath:../services.xml), where the runtime resolution process chooses the “nearest” classpath root and then looks into its parent directory. Classpath configuration changes may lead to the choice of a different, incorrect directory.

You can always use fully qualified resource locations instead of relative paths: for example, file:C:/config/services.xml or classpath:/config/services.xml. However, be aware that you are coupling your application’s configuration to specific absolute locations. It is generally preferable to keep an indirection for such absolute locations — for example, through "${…​}" placeholders that are resolved against JVM system properties at runtime.

可以(但不建议)使用 ./ 相对路径引用父目录中的文件。这样做会创建对当前应用程序之外的文件的依赖关系。特别是,不建议对classpath:url(例如,classpath:../services.xml)使用此引用,其中运行时解析过程选择“最近的”classpath根,然后查看其父目录。Classpath配置更改可能会导致选择不同的、不正确的目录。

您始终可以使用完全限定的资源位置(fully qualified resource location)而不是相对路径:例如,file:C:/config/services.xmlclasspath:/config/services.xml。但是,请注意,您正在将应用程序的配置耦合到特定的绝对位置。通常,最好为此类绝对位置保留一个间接层。例如,通过在运行时根据JVM系统属性解析的“${…}”占位符。

The namespace itself provides the import directive feature. Further configuration features beyond plain bean definitions are available in a selection of XML namespaces provided by Spring — for example, the context and util namespaces.

命名空间本身提供导入指令功能(import directive feature)。更多配置特性(不同于普通bean定义)包含在Spring提供的一系列XML命名空间中。例如,context util命名空间。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

月满闲庭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值