java - 在 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"
那么它能够以某种方式与TopicRepository
other一起拾取Service and Controller
,这不应该是这种情况,因为它们位于不同的包下。
编辑:
封装结构
com.models : 模型
com.repository : 存储库
com.services : 服务
com.controllers : 控制器
com.springboot.springboottest :主类
解决方案
从文档
我们通常建议您将主应用程序类定位在其他类之上的根包中。@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);
}
}
推荐阅读
- r - R:仅从街道名称、城市和州获取邮政编码
- django - 我怎样才能找到用户总数?
- c# - 显示二维数组行列计数器
- javascript - 无法解构“未定义”的属性“纬度”
- generics - 使用@CompileStatic 和泛型类时,构造函数不能应用于“(T)”错误
- python - 当 recycleview 中的 viewclass 按钮被按下时如何移动到下一个屏幕,该按钮在 kivy 的屏幕上?
- python - 导入自己的数据,如 MNIST 或 CIFAR10 load_data()
- c++ - 我们可以在不知道其真实类型的情况下将基类指针转换为派生类指针吗?
- asp.net - 将浏览器 PDF 保存到磁盘
- apache-spark - Elasticsearch如何利用集群?