您应该这样做的方式是利用 OSGi 服务。您可以使用以下内容在 Spring DM 中注册服务(通常在单独的 osgi-context.xml 文件中完成,以确保代码库不依赖于 OSGi 以进行测试。在此示例中,您将拥有一个带有BContext.xml 中定义的 id 诊所,它被称为 OSGi 服务
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<service id="osgiClinic" ref="clinic" interface="org.springframework.petclinic.repository.Clinic" />
</beans:beans>
然后在消费包的 osgi-context.xml 中,您将引用该服务。在下面的示例中,您现在有一个名为 Clinic 的 bean,它使用了第一个 bean 中的代码。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<reference id="clinic" interface="org.springframework.petclinic.repository.Clinic"/>
</beans:beans>
这种做事方式将确保您考虑捆绑包之间的依赖关系,并仅导出其他捆绑包所需的那些服务。