gpt4 book ai didi

java - 我可以从较小的配置文件中编写 Spring 配置文件吗?

转载 作者:IT老高 更新时间:2023-10-28 13:53:38 24 4
gpt4 key购买 nike

我有几个项目都使用一个项目作为数据模型。这些项目中的每一个都有自己的 applicationContext.xml 文件,其中包含一堆重复的数据。

我想要一个 modelContext.xml 文件和另一个用于我的 ui.xml 等。

我可以这样做吗?

最佳答案

来自 Spring Docs (v 2.5.5 Section 3.2.2.1.) :

It can often be useful to split up container definitions into multiple XML files. One way to then load an application context which is configured from all these XML fragments is to use the application context constructor which takes multiple Resource locations. With a bean factory, a bean definition reader can be used multiple times to read definitions from each file in turn.

Generally, the Spring team prefers the above approach, since it keeps container configuration files unaware of the fact that they are being combined with others. An alternate approach is to use one or more occurrences of the element to load bean definitions from another file (or files). Let's look at a sample:

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

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

In this example, external bean definitions are being loaded from 3 files, services.xml, messageSource.xml, and themeSource.xml. All location paths are considered relative to the definition file doing the importing, so services.xml in this case 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 actually ignored, but given that these are considered relative paths, it is probably better form not to use the slash at all. The contents of the files being imported must be valid XML bean definition files according to the Spring Schema or DTD, including the top level element.

关于java - 我可以从较小的配置文件中编写 Spring 配置文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/94542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com