java 我可以从较小的配置文件组成一个 Spring 配置文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/94542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:01:59  来源:igfitidea点击:

Can I compose a Spring Configuration File from smaller ones?

javaspring

提问by Allain Lalonde

I have a handful of projects that all use one project for the data model. Each of these projects has its own applicationContext.xml file with a bunch of repetitive data stuff within it.

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

I'd like to have a modelContext.xml file and another for my ui.xml, etc.

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

Can I do this?

我可以这样做吗?

回答by Nicholas Trandem

From the Spring Docs (v 2.5.5 Section 3.2.2.1.):

来自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.

将容器定义拆分为多个 XML 文件通常很有用。然后加载从所有这些 XML 片段配置的应用程序上下文的一种方法是使用采用多个资源位置的应用程序上下文构造函数。使用 bean 工厂,可以多次使用 bean 定义读取器依次从每个文件读取定义。

通常,Spring 团队更喜欢上述方法,因为它使容器配置文件不知道它们正在与其他文件组合的事实。另一种方法是使用一个或多个元素来从另一个文件(或多个文件)加载 bean 定义。让我们看一个示例:

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

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

在这个例子中,外部 bean 定义是从 3 个文件加载的,services.xml、messageSource.xml 和 themeSource.xml。所有位置路径都被认为是相对于执行导入的定义文件,因此在这种情况下,services.xml 必须与执行导入的文件位于同一目录或类路径位置,而 messageSource.xml 和 themeSource.xml 必须位于资源中位置低于导入文件的位置。如您所见,前导斜杠实际上被忽略了,但考虑到这些被认为是相对路径,最好根本不使用斜杠。根据 Spring Schema 或 DTD,被导入文件的内容必须是有效的 XML bean 定义文件,包括顶级元素。

回答by Asgeir S. Nilsen

We do this in our projects at work, using the classpath* resource loader in Spring. For a certain app, all appcontext files containing the application id will be loaded:

我们在工作中的项目中使用 Spring 中的 classpath* 资源加载器执行此操作。对于某个应用程序,将加载包含应用程序 ID 的所有 appcontext 文件:

classpath*:springconfig/spring-appname-*.xml

回答by Allain Lalonde

Given what Nicholas pointed me to I found this in the docs. It allows me to pick at runtime the bean contexts I'm interested in.

鉴于尼古拉斯向我指出的内容,我在文档中发现了这一点。它允许我在运行时选择我感兴趣的 bean 上下文。

GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new ClassPathResource("modelContext.xml"));
xmlReader.loadBeanDefinitions(new ClassPathResource("uiContext.xml"));
ctx.refresh();

回答by enricopulatzo

Yes, you can do this via the import element.

是的,您可以通过导入元素执行此操作。

<import resource="services.xml"/>

Each element's resource attribute is a valid path (e.g. classpath:foo.xml)

每个元素的资源属性是一个有效的路径(例如 classpath:foo.xml)

回答by Michael

Here's what I've done for one of my projects. In your web.xmlfile, you can define the Spring bean files you want your application to use:

这是我为我的一个项目所做的。在您的web.xml文件中,您可以定义您希望应用程序使用的 Spring bean 文件:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/applicationContext.xml
      /WEB-INF/modelContext.xml
      /WEB-INF/ui.xml
    </param-value>
  </context-param>

If this isn't defined in your web.xml, it automatically looks for /WEB-INF/applicationContext.xml

如果这未在您的 中定义web.xml,它会自动查找/WEB-INF/applicationContext.xml

回答by bpapa

Another thing to note is that although you can do this, if you aren't a big fan of XML you can do a lot of stuff in Spring 2.5 with annotations.

另一件要注意的事情是,虽然您可以这样做,但如果您不是 XML 的忠实粉丝,您可以在 Spring 2.5 中使用注释做很多事情。

回答by Arne Burmeister

Yes, you can using the tag inside the "Master" bean file. But what about the why? Why not listing the files in the contextConfigLocation context param of the wab.xml or als locations array of the bean factory?

是的,您可以使用“Master”bean 文件中的标签。但是为什么呢?为什么不在 bean 工厂的 wab.xml 或 als 位置数组的 contextConfigLocation 上下文参数中列出文件?

I think mutliple files are much easier to handle. You may choose only some of them for a test, simply add rename or remove a part of the application and you may boundle different applications with the same config files (a webapp and a commandline version with some overlapping bean definitions).

我认为多个文件更容易处理。您可以只选择其中的一些进行测试,只需添加重命名或删除应用程序的一部分,您就可以使用相同的配置文件(一个 web 应用程序和具有一些重叠 bean 定义的命令行版本)绑定不同的应用程序。