未找到 Maven 依赖项中的 spring boot @repository bean

Emm*_*uel 3 rest ioc-container maven spring-data-mongodb spring-boot

我正在使用 spring-data-mongoDB (不带 JPA)开发多个 Spring Boot 项目。包含通用(udc-common)组件、存储库和服务以及使用这些组件的其他组件(例如 udc-gateway)的组件。udc-common 库在其他项目中被声明为 Maven 依赖项。(您会在本文末尾找到 Maven 详细配置)

环境结构

udc_common 项目

udc_common.models
udc_common.repositories
udc_common.services
udc_common.interfaces
Run Code Online (Sandbox Code Playgroud)

udc_gateway项目

udc_gateway.controllers
udc_gateway ....

gatewayApplication.java
Run Code Online (Sandbox Code Playgroud)

网关pom.xml

<dependency>
    <groupId>org.open_si</groupId>
    <artifactId>udc-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在 udc-gateway 项目中,我有一个引用 udc-comm SampleService 的其余控制器。运行它时我遇到错误

Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
Run Code Online (Sandbox Code Playgroud)

编码组织是:

  • 控制器(SamplesController)属于网关项目

  • 服务(SampleService)及其存储库(SampleRepository)属于公共项目,在网关项目中被声明为maven依赖项

显然 SampleService 注入确实有效,但其 SampleRepository 依赖项却不起作用。由于通用包是外部包,因此我在网关应用程序主类中相应地设置了 @componentscan

尝试过

@ComponentScan("org.open_si.udc_common")
Run Code Online (Sandbox Code Playgroud)

@ComponentScan({
        "org.open_si.udc_common.repositories",
        "org.open_si.udc_common.services"
})
Run Code Online (Sandbox Code Playgroud)

与 SampleService 相关的SamplesController (网关项目)代码摘录是

@RestController
@RequestMapping("/samples")
public class SamplesController {

    @Autowired
    private SampleService service;
......
Run Code Online (Sandbox Code Playgroud)

SampleService(公共项目)是

@Service
@EnableMongoRepositories
public class SampleService {

    @Autowired
    private SampleRepository sr;

    void insert(BaseSampleEntity sample) {
        this.sr.insert(sample);
    }
    public void delete(BaseSampleEntity sample) {
        this.sr.delete(sample);
    }
.....
Run Code Online (Sandbox Code Playgroud)

或者也尝试过

@Service
@EnableMongoRepositories
public class SampleService {

    private final SampleRepository sr;

    public SampleService(SampleRepository repository) {
        sr = repository;
    }

    void insert(BaseSampleEntity sample) {
        this.sr.insert(sample);
    }
    public void delete(BaseSampleEntity sample) {
        this.sr.delete(sample);
    }
Run Code Online (Sandbox Code Playgroud)

SampleRepository(公共项目)是

// @Repository (JPA only)
public interface SampleRepository extends MongoRepository<BaseSampleEntity, String> {

    List<BaseSampleEntity> findByNodeUuid(String Uuid, Sort sort, Pageable pageable);

    List<BaseSampleEntity> findByFieldUuid(String Uuid, Sort sort, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

所以引发了异常

Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
Run Code Online (Sandbox Code Playgroud)

让我觉得 spring boot IOC 过程出了问题。任何想法 ?

在此先感谢您的帮助。


udc-common pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.open_si</groupId>
    <artifactId>udc-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>UDC common</name>
    <packaging>jar</packaging>
    <description>Common resources for Universal Data Collector</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
<!--                    <source>1.8</source>-->
<!--                    <target>1.8</target>-->
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

udc-网关 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.open_si</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Gateway</name>
    <description>Gateway for UDC project</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.open_si</groupId>
            <artifactId>udc-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

Kum*_*r V 5

尝试在 Spring Boot 主应用程序类中添加以下注释(即使用 SpringBootApplication - GateWayApplication.java 注释的类)

@EnableMongoRepositories(basePackages = "org.open_si.udc_common")
@ComponentScan("org.open_si.udc_common.services")
@EntityScan(basePackages ="org.open_si.udc_common.models") 
Run Code Online (Sandbox Code Playgroud)

另外,正如您所提到的,您不需要使用 @Repository 注释存储库类,因为它已经由 spring data 管理

更新: @SpringBootApplication 本身嵌入了 ComponentScan 注释,因此它会自动扫描所有子目录下的所有类。

希望你的目录结构有点类似于

com.example
--controllers
    -- SamplesController
--service
    -- SampleService
SampleApplication (this is the class that contains @SpringBootApplication & @EnableMongoRepositories)
Run Code Online (Sandbox Code Playgroud)