繁体   English   中英

用于API,接口和实现的Spring Boot Separate模块

[英]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-apispring-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.javaspring-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM