欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-08 19:10:20
...

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定义。

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

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.

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

可以(但不建议)使用 ./ 相对路径引用父目录中的文件。这样做会创建对当前应用程序之外的文件的依赖关系。特别是,不建议对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命名空间。