简体   繁体   English

如何将接口和实现分离到不同的模块中

[英]How to separate interface and implementation into different modules

Hi everyone i am creating a project in Spring Boot with multi-module maven, what i would like to obtain is to seperate the interface and implementation in two different modules, like this:大家好,我正在使用多模块 maven 在 Spring Boot 中创建一个项目,我想要获得的是在两个不同的模块中分离接口和实现,如下所示:

  1. Module A:模块 A:
   public interface MyTestInterface{
   String testMethod();
}
  1. Module B :模块 B:
  public class MyTestImpl implements MyTestInterface{
  @Override
  String testMethod(){
    return "foo";
  };

}

I can't get this result, could you give me an example of how to get it?我无法得到这个结果,你能给我一个如何得到它的例子吗? Also is it a good thing to do this?这样做也是一件好事吗? As for the poms at the moment I only have a B versus A addiction in the poms.至于目前的 poms,我对 poms 只有 B 与 A 的瘾。

        <dependency>
            <groupId>test.some</groupId>
            <artifactId>B</artifactId>
        </dependency>

The pom of the project that contains A and B module is some likes this:包含 A 和 B 模块的项目的 pom 是这样的:

    <modules>
        <module>A</module>
        <module>B</module>

    </modules>

First let's clarify something here.首先让我们在这里澄清一些事情。

Your question has to do with apache-maven modules, not with java modules.您的问题与apache-maven模块有关,而不是与 java 模块有关。

So now that we are clear let us focus on the issue that you have.因此,既然我们已经清楚了,让我们专注于您遇到的问题。

From comments:来自评论:

I couldn't get what I want, i can't import my interface from module A to B我无法得到我想要的,我无法将我的接口从模块 A 导入到 B

Yes because what you use is是的,因为您使用的是

    <dependency>
        <groupId>test.some</groupId>
        <artifactId>B</artifactId>
    </dependency>

which means that you have this dependency in module A. So you try to import Module B into Module A .这意味着您在模块 A 中有这种依赖关系。因此您尝试将Module B导入到Module A中。

What however your requirement needs is exactly the opposite.但是,您的要求恰恰相反。 You need Module A inside Module B .您需要Module AModule B

For this you have to go to .pom of Module B and add the following为此,您必须转到Module B.pom并添加以下内容

    <dependency>
        <groupId>test.some</groupId>
        <artifactId>A</artifactId>
    </dependency>

and also remove the previous dependency that you have in .pom inside Module A because if not you will have circular dependency issue.并删除您在Module A内的.pom中的先前依赖项,因为如果没有,您将遇到循环依赖问题。

You need a parent maven project with packaging as POM and modules under them!您需要一个父 maven 项目,其下打包为POM和模块!

Follow this按照这个

  1. Create a maven project with packaging type as POM (It should look something like this, sorry for formatting issues)创建一个打包类型为 POM 的 maven 项目(它应该看起来像这样,对于格式问题很抱歉)

    <modelVersion>4.0.0</modelVersion> <groupId>com.multimodule</groupId> <artifactId>com.example.multimodule</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>interface-module</module> <module>implementation-module</module> </modules>

  2. Create an interface-module with packaging JAR with parent pointing to project you created in point 1 (See below)创建一个带有包装 JAR 的接口模块,其父指向您在第 1 点创建的项目(见下文)

    <parent> <groupId>com.multimodule</groupId> <artifactId>com.example.multimodule</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.example.interface-module</groupId> <artifactId>interface-module</artifactId> <version>1.0</version> <packaging>jar</packaging>

  3. Create an implementation-module with packaging JAR with parent pointing to project you created in point 1 and dependency to project created in point 2创建一个带有打包 JAR 的实现模块,其父项指向您在第 1 点创建的项目以及对第 2 点创建的项目的dependency

    <parent> <groupId>com.multimodule</groupId> <artifactId>com.example.multimodule</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.example.implementation-module</groupId> <artifactId>implementation-module</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.example.interface-module</groupId> <artifactId>interface-module</artifactId> <version>1.0</version> </dependency> </dependencies>

Likewise for other modules !其他模块也是如此! I was able to achieve what you asked , let me know if you need a reference, will upload and send GitHub link!我能够实现您的要求,如果您需要参考,请告诉我,将上传并发送 GitHub 链接!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于API,接口和实现的Spring Boot Separate模块 - Spring Boot Separate modules for api, interface and implementation 如何将存储库和存储库实现划分为不同的模块? - How to split Repository and Repository implementation to different modules? 如何为不同的类编写一个接口实现? - How to write one interface implementation for different classes? 出现异常时如何使用接口将接口与技术实现分开 - How to separate interface from technical implementation using Interfaces when there are Exceptions Sprint 数据使用多个数据模块在运行时注入不同实现的相同接口 - Sprint Data Use Multiple Data Modules To Inject Same Interface with Different Implementation at Runtime 如何访问同一接口的不同实现的不同属性 - How to access different attributes of different implementation of the same interface 多个maven模块分开测试,api和实现 - Multiple maven modules to separate tests, api and implementation maven - 用于接口和Spring实现的独立模块 - maven - separate modules for interfaces and implementation with Spring 通过不同的JVM实现接口 - Interface implementation through different JVMs 如何配置 Maven 项目为接口的每个实现生成不同的 jar - How to configure a Maven project to generate a different jar for each implementation of an interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM