简体   繁体   English

@Autowired spring 启动多模块项目上的问题

[英]@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.我正在尝试使用 spring 引导创建一个多模块,我创建了一个父模块和 2 个子模块。

This is the pom.xml file of the parent module这是父模块的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>

I have 2 child module 'portalAPICore','portalAPIPersistanceLayer'我有 2 个子模块 'portalAPICore'、'portalAPIPersistanceLayer'

I need to autowire the repository class on portalAPIPersistanceLayer to my service class in portalAPICore我需要将 portalAPIPersistanceLayer 上的存储库 class 自动连接到我在 portalAPICore 中的服务 class

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. com.example.demo.portal.portalAPICore.Service.ServiceClassTest 中的字段 testCouchRepo 需要找不到类型为“com.example.demo.portal.portalAPIPersistanceLayer.repo.TestCouchRepo”的 bean。

The injection point has the following annotations:注入点有以下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true) @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.考虑在您的配置中定义“com.example.demo.portal.portalAPIPersistanceLayer.repo.TestCouchRepo”类型的 bean。

This is the TestCouchRepo class on portalAPIPersistanceLayer module这是 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();
    
}

This the service class were I getting the issue这个服务 class 是我遇到的问题

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

This is the pom file of portalAPICore module in which I have added the dependency of portalAPIPersistanceLayer module这是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>

This is the main class这是主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除了为组件( @ComponentScan )共享相同的basePackages之外,您还必须像这样启用相同的 JpaRepository basePackages

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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