我正在尝试构建一个项目,它有三个用于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。pom.xml
的spring-multi-module
文件
<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>
pom.xml
的spring-api
<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)
而不是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>
一切都应该工作得很好。