2

我的项目由多个 Spring 子项目组成:

  • 服务1
  • 服务2
  • 服务3

每个 Service 对内部的其他 Bean 都有多个依赖项,因此每个 Service 都有一个 applicationContext.xml 将 Service 连接在一起。

我使每个子项目都成为独立的 maven 构建,我认为我可以创建一个 AllServicesTogether 应用程序来将这些服务{1..3} 连接在一起。

这通过向这些服务添加 Maven 依赖项来工作。

<dependencies>
    <dependency>
        <groupId>org.myproject</groupId>
        <artifactId>myproject-service{1..3}</artifactId>
        <version>0.1-SNAPSHOT</version>
    </dependency>
    ...
</dependencies>

但是在 AllServicesTogether 应用程序中,所有子服务的连接都丢失了。我猜 Subservices 不是用 Subservice ApplicationContext 编译的,而是使用 AllServicesTogether ApplicationContext。

想法是封装 SubSerivces 的所有连接,并使用以下方法简单地连接 AllServicesTogether:

<beans ..>
    <bean class="org.myproject.service1.Service1"/>
    <bean class="org.myproject.service1.Service2"/>
    <bean class="org.myproject.service1.Service3"/>
</beans>

我从花费数小时的较大项目中创建了这些子项目。是否可以使用这种接线方法,或者我是否需要包含所有这些服务中的 context.xml?

4

2 回答 2

3

您需要包含这些服务中的 context.xml。最好在 AllServicesTogether-context.xml 中使用“导入”来完成:

<import resource="classpath*:/META-INF/spring/service1-context.xml" />
<import resource="classpath*:/META-INF/spring/service2-context.xml" />
<import resource="classpath*:/META-INF/spring/service3-context.xml" />
于 2012-05-09T18:59:59.977 回答
1

使用类路径*:/META-INF/spring/*-context.xml

参考文献:</p>

  1. Spring 应用上下文加载技巧
  2. 带有来自 Jar 的多个 XML 文件的 Spring ApplicationContext
于 2012-10-18T02:37:12.293 回答