Spring Boot用于api,接口和实现的独立模块

问题描述 投票:0回答:2

我正在尝试构建一个项目,它有三个用于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。

pom.xmlspring-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.xmlspring-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.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)而不是SpringAppApiConfig,那么它的作品也是如此,这意味着我将所有实体和存储库传播到我不想要的spring-api模块。

java spring spring-boot multi-module spring-bean
2个回答
1
投票

默认情况下,Spring会扫描@SpringBootApplication类的子包中的所有类。由于UserController,UserService等类不在您需要添加的子包中

@ComponentScan(basePackages = {"com.example"})
@SpringBootApplication
public class SpringAppApi {

0
投票

在您的项目中,您有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>

一切都应该工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.