![](/img/trans.png)
[英]Spring Boot Separate modules for api, interface and implementation
[英]How to separate interface and implementation into different modules
大家好,我正在使用多模塊 maven 在 Spring Boot 中創建一個項目,我想要獲得的是在兩個不同的模塊中分離接口和實現,如下所示:
public interface MyTestInterface{
String testMethod();
}
public class MyTestImpl implements MyTestInterface{
@Override
String testMethod(){
return "foo";
};
}
我無法得到這個結果,你能給我一個如何得到它的例子嗎? 這樣做也是一件好事嗎? 至於目前的 poms,我對 poms 只有 B 與 A 的癮。
<dependency>
<groupId>test.some</groupId>
<artifactId>B</artifactId>
</dependency>
包含 A 和 B 模塊的項目的 pom 是這樣的:
<modules>
<module>A</module>
<module>B</module>
</modules>
首先讓我們在這里澄清一些事情。
您的問題與apache-maven
模塊有關,而不是與 java 模塊有關。
因此,既然我們已經清楚了,讓我們專注於您遇到的問題。
來自評論:
我無法得到我想要的,我無法將我的接口從模塊 A 導入到 B
是的,因為您使用的是
<dependency>
<groupId>test.some</groupId>
<artifactId>B</artifactId>
</dependency>
這意味着您在模塊 A 中有這種依賴關系。因此您嘗試將Module B
導入到Module A
中。
但是,您的要求恰恰相反。 您需要Module A
內Module B
。
為此,您必須轉到Module B
的.pom
並添加以下內容
<dependency>
<groupId>test.some</groupId>
<artifactId>A</artifactId>
</dependency>
並刪除您在Module A
內的.pom
中的先前依賴項,因為如果沒有,您將遇到循環依賴問題。
POM
和模塊!按照這個
創建一個打包類型為 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>
創建一個帶有包裝 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>
創建一個帶有打包 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>
其他模塊也是如此! 我能夠實現您的要求,如果您需要參考,請告訴我,將上傳並發送 GitHub 鏈接!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.