是否可以为每个 jar 文件使用带有附加信息的 spring 配置 XML 文件?
我的主 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这非常简单。
在主 Spring 上下文文件中,添加如下内容:
注意通配符语法。这将扫描类路径的根目录,查找名为
config.xml
的任何资源,并将它们导入到您的主上下文中。因此,每个 JAR 的根目录中都可以有一个
config.xml
,这就会发生。当然,这些 config.xml 文件可以执行自己的导入,等等。Yes, this is pretty straightforward.
In your main Spring context file, add something like this:
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. Thoseconfig.xml
files can, of course, perform their own imports, and so on.