如何使 2 spring 个项目共享相同的代码库

How to make 2 spring projects share the same code base

我在 Spring MVC 中有 2 个 Web 项目,它们相似并且具有相同的服务和模型。我想拆分代码以便有一个共同的核心。

我使用共享的所有服务和模型创建了一个新项目 (CORE),并按照此处指定的方式将其导出为 jar (Auto-wiring annotations in classes from dependent jars),但未在 CHILD 项目中扫描和自动连接组件.

那么,我的问题是:

除了 jars 之外,还有哪些其他选项可以实现自动连接?

在 spring 个项目中拆分代码库和共享核心组件的最佳做法是什么?

类似于下面的内容。有关详细信息,请参阅 http://books.sonatype.com/mvnex-book/reference/multimodule.html

Maven 父模块/pom.xml:

...
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>X.X-TAG</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>child1</module>
</modules>
...

Maven 核心模块:/core/pom.xml:

...
<parent>
<artifactId>parent</artifactId>
<groupId>com.example</groupId>
<version>X.X-TAG</version>
</parent>
<packaging>jar</packaging>
<artifactId>core</artifactId>
...

Maven child1 模块:/child1/pom.xml:

...
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>X.X-TAG</version>
</parent>
<packaging>war</packaging>
<artifactId>child1</artifactId>
<dependencies>
<dependency>
    <groupId>${parent.groupId}</groupId>
    <artifactId>core</artifactId>
    <version>${parent.versionId}</version>
</dependency>
</dependencies>
...

我会使用跨上下文。通过这种方式,您可以在许多应用程序中自动装配您的 bean,并将此模块与其他模块分开,当然,您希望在应用程序之间共享的代码不会重复。

我建议看看这个指南:

http://blog.imaginea.com/cross-context-communication-between-web-applications/

来自 Spring 博客: http://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/