简体   繁体   中英

Spring Boot Separate modules for api, interface and implementation

I am trying to build one project which has three sub-modules for api(web), interface and implementation.

Directory tree structure is like

spring-multi-module
--spring-api
--spring-service-server
--spring-service-stub
  • An idea is module spring-api would just contain the code related to controllers and web, and pom.xml have spring web and spring-service-stub dependencies.
  • The Module spring-service-server will contain the code related to database configuration and all service implementations, and pom.xml will contains the database and spring-service-stub dependencies.
  • and the module spring-service-stub will contains only interfaces and VOs which are used by the spring-api and spring-service-server .

pom.xml file of the 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 of 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 is the interface in spring-service-stub module and its implementation is on the spring-service-server module. UserController.java have the autowired UserService object.

And the problem is when I am trying to run the SpringBootApplication class from the spring-api then gets the below error on log

***************************
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.

Full code also added on github, you can find from https://github.com/vinitsolanki/spring-multi-module

Simply if I add use @Import({SpringAppStub.class, SpringAppServer.class}) instead of @Import(SpringAppStub.class) in SpringAppApiConfig then also its works, It means I am spreading all entities and repository to the spring-api module which I don't want to.

By default, Spring scans all classes in the sub package of @SpringBootApplication class. Since the UserController, UserService etc. classes are not in the sub-packages you need to add

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

In your project you have 3 modules

spring-api 
spring-service-server
spring-service-stub

spring-service-server is dependent on spring-service-stub

spring-api is dependent on spring-service-stub

If you see this setup there is no involvement of spring-service-server

Ideally, it should be like this

spring-api should be dependent on spring-service-server

You can change your spring-api => pom.xml

remove stub dependency and add

<dependency>
    <groupId>com.example</groupId>
    <artifactId>spring-service-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

And all should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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