扩展基于 spring 的应用程序
我有一个基于 spring 的 Web 服务。我现在想为它构建一种插件,用 beans 来扩展它。我现在在 web.xml 中的内容是:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/*-configuration.xml</param-value>
</context-param>
我的核心应用程序有 main-configuration.xml
,它声明了它的 bean。我的插件应用程序有 plugin-configuration.xml
,它声明了其他 bean。现在,当我部署时,我的构建将 plugin.jar
部署到 /WEB-INF/lib/
并将 plugin-configuration.xml
复制到 >/WEB-INF/classes/
全部位于 main.war
下。
这一切都很好(虽然我认为可能有更好的解决方案),但是当我开发插件时,我不希望 Eclipse 中有两个具有依赖关系的项目。我希望将 main.jar
作为库包含在内。但是,不会自动发现 main.jar
中的 web.xml
。我该怎么做?豆注射?某种 Bean 发现?还有别的事吗?
注意:我希望在生产中拥有多个不同的插件,但是每个插件的开发都将针对纯 main.jar
谢谢。
I have a spring-based Web Service. I now want to build a sort of plugin for it that extends it with beans. What I have now in web.xml is:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/*-configuration.xml</param-value>
</context-param>
My core app has main-configuration.xml
which declares its beans. My plugin app has plugin-configuration.xml
which declares additional beans. Now when I deploy, my build deploys plugin.jar
into /WEB-INF/lib/
and copies plugin-configuration.xml
into /WEB-INF/classes/
all under main.war
.
This is all fine (although I think there could be a better solution), but when I develop the plugin, I don't want to have two projects in Eclipse with dependencies. I wish to have main.jar
that I include as a library. However, web.xml
from main.jar
isn't automatically discovered. How can I do this? Bean injection? Bean discovery of some sort? Something else?
Note: I expect to have multiple different plugins in production, but development of each of them will be against pure main.jar
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为有一个更简单的方法:
在您的主机应用程序(网络应用程序)中定义类似以下
contextConfigLocation
参数:关键部分是
classpath*:
因为它会导致扫描匹配以下模式的配置文件的整个类路径。如果您只使用classpath:
,查找将在找到的第一个文件处停止。如果加星号不正确,它也不会遍历 JAR。第二个关键部分是至少有一个非通配符基本路径以使查找正常工作。这是由于
Classloader
特征仅可靠地返回包含基本路径的所有资源(请参阅 Javadoc onPathMatchingResourcePatternResolver
了解详细信息)。现在您需要对插件项目做的是将
plugin-configuration.xml
放在/META-INF/foo/
中,将其打包为 JAR 并将其放入您的类路径(如果您使用 Maven 构建,只需添加依赖项)。在应用程序启动时,Spring 现在还将从插件中获取所有配置文件,并从中构建“applicationContext”。
您可能还想查看我在 Spring 应用程序模块化方面所做的另一篇文章: 如何进行多模块弹簧配置?
I think there is a simpler approach:
In your host application (the webapp) define something like the following
contextConfigLocation
parameter:The crucial parts are
classpath*:
as it results in scanning the entire classpath for config files matching the following pattern. If you only useclasspath:
the lookup will stop at the first file found. It also won't traverse JARs if not starred correctly.The second crucial part is having at least one non-wildcarded basepath for the lookup to work. This is due to
Classloader
traits that only reliably returns all resources if they contain a base path (see the Javadoc onPathMatchingResourcePatternResolver
for details).What you now have to do with your plugin projects is place your
plugin-configuration.xml
in/META-INF/foo/
, package that as JAR and place it into your classpath (in case you build with Maven just add the dependency).On application start Spring will now also pickup all config files from your plugins and build the ÀpplicationContext` from em.
You also might wanna checkout another post I did on application modularity with Spring: How do you make a multi-module spring configuration?
尝试使用
classpath:*-configuration.xml
而不是/WEB-INF/classes/*-configuration.xml
您还可以列出配置文件,每个文件占一个新行。
您必须确保 main.jar 最终位于 WEB-INF/lib 中,如果您不使用 maven,您可以在 eclipse 中通过将 webapp 标记为依赖于创建 main.jar 的项目来完成此操作项目属性。
Instead of
/WEB-INF/classes/*-configuration.xml
, tryclasspath:*-configuration.xml
You can also list configuration files, each on a new line.
You will have to make sure that main.jar ends up in WEB-INF/lib, if you're not using maven you can do this in eclipse by marking you webapp as dependent on the project that creates the main.jar, via the project properties.