簡體   English   中英

@Autowired spring 啟動多模塊項目上的問題

[英]@Autowired Issue on spring boot multi module project

我正在嘗試使用 spring 引導創建一個多模塊,我創建了一個父模塊和 2 個子模塊。

這是父模塊的pom.xml文件

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

我有 2 個子模塊 'portalAPICore'、'portalAPIPersistanceLayer'

我需要將 portalAPIPersistanceLayer 上的存儲庫 class 自動連接到我在 portalAPICore 中的服務 class

但是我遇到了這個問題

com.example.demo.portal.portalAPICore.Service.ServiceClassTest 中的字段 testCouchRepo 需要找不到類型為“com.example.demo.portal.portalAPIPersistanceLayer.repo.TestCouchRepo”的 bean。

注入點有以下注解:

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

行動:

考慮在您的配置中定義“com.example.demo.portal.portalAPIPersistanceLayer.repo.TestCouchRepo”類型的 bean。

這是 PortalAPIPersistanceLayer 模塊上的 TestCouchRepo class

    @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();
    
}

這個服務 class 是我遇到的問題

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

@Component
public class ServiceClassTest {
    
    @Autowired
    TestCouchRepo testCouchRepo;

這是portalAPIPersistanceLayer模塊的pom.xml文件

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

這是portalAPICore模塊的pom文件,我在其中添加了portalAPIPersistanceLayer模塊的依賴

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

這是主class

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

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

    }
}

我沒有發現這里的流程有任何問題,誰能幫我解決這個問題

除了為組件( @ComponentScan )共享相同的basePackages之外,您還必須像這樣啟用相同的 JpaRepository basePackages

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM