【问题标题】:How to make 2 spring projects share the same code base如何使 2 个 spring 项目共享相同的代码库
【发布时间】:2015-04-24 02:55:27
【问题描述】:

我在 Spring MVC 中有 2 个类似的 Web 项目,它们具有相同的服务和模型。我想拆分代码以获得共同的 CORE。

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

所以,我的问题是:

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

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

【问题讨论】:

  • 父 Maven/Gradle 项目。
  • 能否提供更多信息
  • 将您需要的包从核心 jars 添加到应用程序上下文的组件扫描中,或者如果它们在 xml 中定义,您需要将它们添加到您的应用程序上下文或 Web 应用程序上下文中。你确定它们在类路径中吗?

标签: java spring spring-mvc


【解决方案1】:

如下所示。请参阅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>
...

【讨论】:

    【解决方案2】:

    我会使用跨上下文。这样,您可以在许多应用程序中自动装配 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/

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      相关资源
      最近更新 更多