Wiring multiple independent Spring Applications us

2019-07-16 12:21发布

问题:

My project consists of multiple Spring subprojects:

  • Service1
  • Service2
  • Service3

Every Service has multiple dependencies to other Beans inside, so every Service has an applicationContext.xml wiring the Service together.

I made every subproject an standalone maven build and i thought i can create a AllServicesTogether Application to wire those Service{1..3} together.

This works by adding maven dependencies to those Services.

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

But inside the AllServicesTogether Application, all wiring of the SubServices is lost. I guess Subservices aren't compiled with the Subservice ApplicationContext, but use the AllServicesTogether ApplicationContext.

The Idea is to encapsulate all wiring of SubSerivces and simply wire the AllServicesTogether by using:

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

I created those subprojects from the bigger project spending hours on it. Is it possible to use this wiring method or do i need to include the context.xml's from all those Services?

回答1:

You need to include the context.xml's from those services. This is best done using 'import' in your 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" />


回答2:

use classpath*:/META-INF/spring/*-context.xml

References:

  1. Spring application context loading tricks
  2. Spring ApplicationContext with multiple XML Files from Jar