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

Mic*_*ash 5 java spring spring-mvc

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

我创建了一个新项目 (CORE),其中包含共享的所有服务和模型,并将其导出为此处指定的 jar(从依赖 jar 中的类中自动装配注释),但在子项目中不会扫描和自动装配组件。

所以,我的问题是:

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

在 Spring 项目中拆分代码库并共享核心组件的最佳实践是什么?

Nei*_*gan 2

像下面这样的东西。请参阅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>
...
Run Code Online (Sandbox Code Playgroud)

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

...
<parent>
<artifactId>parent</artifactId>
<groupId>com.example</groupId>
<version>X.X-TAG</version>
</parent>
<packaging>jar</packaging>
<artifactId>core</artifactId>
...
Run Code Online (Sandbox Code Playgroud)

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>
...
Run Code Online (Sandbox Code Playgroud)