是否可以为每个 jar 文件使用带有附加信息的 spring 配置 XML 文件?

发布于 2024-12-24 18:39:58 字数 430 浏览 7 评论 0原文

我的主 Spring 配置 XML 文件指定了如下内容:

在此扫描期间找到的组件可以位于多个 jar 之一中我的类路径上的文件。我希望能够在每个 jar 文件中包含额外的 spring 配置 XML 文件,这些文件提供有关每个单独服务/组件的更多信息(可能是一些属性之类的),但我不希望这些东西与我的项目紧密耦合。主应用程序的配置文件。

有没有办法做到这一点,还是我运气不好?

理想情况下,我希望能够根据 JAR 文件中的名称和/或位置以某种方式自动检测 JAR 文件中 XML 文件的存在。

我知道类似这样的事情可以使用 META-INF 完成,但这远远超出了我的专业领域。

My main Spring Configuration XML file specifies something like this:

<context:component-scan base-package="com.application" />

The components found during this scan can be in one of many jar files on my classpath. I'd like to be able to have additional spring configuration XML files in each jar file that provide more information about each individual service/component (maybe some properties and whatnot), but I don't want these things to be tightly coupled to my main application's configuration file.

Is there a way to do this, or am I out of luck?

Ideally, I'd like to be able to somehow autodetect the existence of the XML file in the JAR file, based on name and/or location in the JAR file.

I know some things like this can be done with META-INF, but that's far beyond my area of expertise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-12-31 18:39:58

理想情况下,我希望能够根据 JAR 文件中的名称和/或位置以某种方式自动检测 JAR 文件中 XML 文件的存在。

是的,这非常简单。

在主 Spring 上下文文件中,添加如下内容:

<import resource="classpath*:config.xml"/>

注意通配符语法。这将扫描类路径的根目录,查找名为 config.xml 的任何资源,并将它们导入到您的主上下文中。

因此,每个 JAR 的根目录中都可以有一个 config.xml,这就会发生。当然,这些 config.xml 文件可以执行自己的导入,等等。

Ideally, I'd like to be able to somehow autodetect the existence of the XML file in the JAR file, based on name and/or location in the JAR file.

Yes, this is pretty straightforward.

In your main Spring context file, add something like this:

<import resource="classpath*:config.xml"/>

Note the wildcard syntax. This will scan the root of the classpath looking for any resources called config.xml, and will import them into your main context.

So your JARs can each have a config.xml in their root directory, and it'll just happen. Those config.xml files can, of course, perform their own imports, and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文