[英]Spring Boot Separate modules for api, interface and implementation
我正在嘗試建立一個項目,該項目具有api(web),接口和實現的三個子模塊。
目錄樹的結構就像
spring-multi-module
--spring-api
--spring-service-server
--spring-service-stub
spring-api
將只包含與控制器和Web相關的代碼,而pom.xml
具有spring web和spring-service-stub
依賴性。 spring-service-server
將包含與數據庫配置和所有服務實現相關的代碼,而pom.xml
將包含數據庫和spring-service-stub
依賴項。 spring-service-stub
模塊將僅包含spring-api
和spring-service-server
使用的接口和VO。 spring-multi-module
pom.xml
文件
<modules>
<module>spring-api</module>
<module>spring-service-server</module>
<module>spring-service-stub</module>
</modules>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
spring-api
pom.xml
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-api</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-service-stub</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
UserSerivce.java
是spring-service-stub
模塊中的接口,其實現在spring-service-server
模塊上。 UserController.java
具有自動裝配的UserService
對象。
問題是當我嘗試從spring-api
運行SpringBootApplication類時,在日志中出現以下錯誤
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in com.example.demo.api.controller.UserController required a bean of type 'com.example.demo.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.service.UserService' in your configuration.
完整的代碼也添加在github上,您可以從https://github.com/vinitsolanki/spring-multi-module找到
簡而言之,如果我在@Import({SpringAppStub.class, SpringAppServer.class})
添加@Import({SpringAppStub.class, SpringAppServer.class})
而不是@Import(SpringAppStub.class)
,那么它的工作原理SpringAppApiConfig
如此,這意味着我將所有實體和存儲庫散布到spring-api
模塊中我不想
默認情況下,Spring掃描@SpringBootApplication類的子包中的所有類。 由於UserController,UserService等類不在子包中,因此您需要添加
@ComponentScan(basePackages = {"com.example"})
@SpringBootApplication
public class SpringAppApi {
在您的項目中,您有3個模塊
spring-api
spring-service-server
spring-service-stub
spring-service-server依賴於spring-service-stub
spring-api依賴於spring-service-stub
如果您看到此設置,則表明spring-service-server
沒有參與
理想情況下,應該像這樣
spring-api應該依賴於spring-service-server
您可以更改您的spring-api => pom.xml
刪除stub
依賴性並添加
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-service-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
一切都應該很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.