首页 > 解决方案 > 在 Spring Boot 中无法检测到存储库注释

问题描述

我在包上有一个 Repository 类:

com.repository

我已经@ComponentScan("com.*")在 Spring Boot 的主类中定义了。

但是,在运行应用程序时,我得到了异常

考虑在你的配置中定义一个“com.repository.TopicRepository”类型的bean。

以下是课程:

主题库

package com.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.models.Topic;


@Repository
public interface TopicRepository extends CrudRepository<Topic,String>{



}

应用

package com.example.springboot.springboot.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.*")
public class Application {

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

}

POM.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springboot</groupId>
<artifactId>springboot.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot.test</name>
<description>Demo project for Spring Boot</description>


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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <scope>runtime</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>
</dependencies>

<properties>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

我已经试过了@ComponentScan。如果我将其更改为scanBasePackages"com.repository"那么它能够以某种方式与TopicRepositoryother一起拾取Service and Controller,这不应该是这种情况,因为它们位于不同的包下。

编辑:

封装结构

com.models : 模型

com.repository : 存储库

com.services : 服务

com.controllers : 控制器

com.springboot.springboottest :主类

标签: javaspringeclipsespring-boot

解决方案


文档

我们通常建议您将主应用程序类定位在其他类之上的根包中。@SpringBootApplication 注解通常放在你的主类上,它为某些项目隐式定义了一个基本的“搜索包”

同样正如 chrylis -on strike在评论中指出的那样,基本包应该被定义为com. 扫描将从包com及其所有子包开始

@ComponentScan ("com")

更新

基于共享的包结构。

@SpringBootApplication(scanBasePackages = {"com.models","com.repository","com.services","com.controllers"})
public class Application {

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

}

或者

@SpringBootApplication(scanBasePackages = "com")
public class Application {

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

}

应该管用。

最好的选择是将类移动到具有以下注释Application的包中。com

@SpringBootApplication
public class Application {

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

}

推荐阅读