简体   繁体   中英

@Autowired Issue on spring boot multi module project

I am trying to create a multi-module with spring boot, I have created a parent module and 2 child module.

This is the pom.xml file of the parent module

<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo.portal</groupId>
<artifactId>portalAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
    <module>portalAPICore</module>
    <module>portalAPIPersistanceLayer</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2-SNAPSHOT</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>11</java.version>
    <!-- <spring-boot.repackage.skip>true</spring-boot.repackage.skip> -->
</properties>

<dependencies>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- <configuration> <skip>true</skip> </configuration> -->
            </plugin>
        </plugins>
    </pluginManagement>
</build>

I have 2 child module 'portalAPICore','portalAPIPersistanceLayer'

I need to autowire the repository class on portalAPIPersistanceLayer to my service class in portalAPICore

But am getting this issue

Field testCouchRepo in com.example.demo.portal.portalAPICore.Service.ServiceClassTest required a bean of type 'com.example.demo.portal.portalAPIPersistanceLayer.repo.TestCouchRepo' that could not be found.

The injection point has the following annotations:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.example.demo.portal.portalAPIPersistanceLayer.repo.TestCouchRepo' in your configuration.

This is the TestCouchRepo class on portalAPIPersistanceLayer module

    @Repository
public interface TestCouchRepo extends ReactiveCrudRepository<Test, String>{
    
    String designDocument = "pandalytics", viewName = "view_allTests";
    String DESIGN_DOCUMENT="view_allTESTs";
    

    @View(designDocument = TestCouchRepo.designDocument, viewName = TestCouchRepo.viewName)
    Flux<Test> findAll();
    
}

This the service class were I getting the issue

import com.knowesis.sift.portal.portalAPIPersistanceLayer.repo.TestCouchRepo;

@Component
public class ServiceClassTest {
    
    @Autowired
    TestCouchRepo testCouchRepo;

This is the pom.xml file of the portalAPIPersistanceLayer module

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example.demo.portal</groupId>
    <artifactId>portalAPI</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>portalAPIPersistanceLayer</artifactId>
  
  <packaging>jar</packaging>
</project>

This is the pom file of portalAPICore module in which I have added the dependency of portalAPIPersistanceLayer module

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example.demo.portal</groupId>
        <artifactId>portalAPI</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>portalAPICore</artifactId>
    
    <packaging>jar</packaging>

    <dependencies>
        
        <dependency>
                    <groupId>com.example.demo.portal</groupId>
                    <artifactId>portalAPIPersistanceLayer</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
            </dependency>
        
            <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-couchbase</artifactId>
            <version>3.0.9.RELEASE</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <dependency>
            <groupId>com.couchbase.client</groupId>
            <artifactId>java-client</artifactId>
            <version>2.5.4</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency> -->
        
        
    </dependencies>

</project>

This is the main class

@SpringBootApplication
@ComponentScan(basePackages = { "com.example.demo.portal.*" })
public class PortalAPICore {

    public static void main(String[] args) {
        SpringApplication.run(PortalAPICore.class);

    }
}

I didn't found any wrong here with the flow, can anyone help me to sort out this issue

In addition of sharing the same basePackages for components ( @ComponentScan ), you must also enable the same JpaRepository basePackages like this

@EnableJpaRepositories(basePackages = {"com.example.demo.portal.*"})

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