【发布时间】:2021-10-03 11:57:12
【问题描述】:
我在启动 SpringBootApplication 时遇到了这个错误。
Description:
Field XXX in com.helloworld.www.batch.SomeServiceImpl required a bean of type 'com.helloworld.www.batch.repository.SomeRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.helloworld.www.batch.repository.SomeRepository' in your configuration.
这是我的项目布局,其中有我的 SpringBootApplication 和存储库。我用“@SpringBootApplication”注释了我的 SpringBootApplication,但它找不到“SomeRepository”bean 类。
我是否必须用“EnableJpaRepositories”注释它并指出存储库包,然后它才能检测到“SomeRepository”bean 类?
com.helloworld.www
+SpringBootApplicationName.java
com.helloworld.www.batch.repository
+SomeRepositoryName.java
更新帖子,我已经用@Repository注释了我的Repository,它只包含一个没有任何实现类的接口。
@Repository
public interface SomeRepository extends PagingAndSortingRepository<SomeModel, Integer> { }
这是我在“com.helloworld.www”包下的主要应用程序类
@SpringBootApplication
public class SomeApplication {
public static void main(String[] args) {
SpringApplication.run(SomeApplication.class);
}
}
【问题讨论】:
-
你有没有用@Repository注解对repository接口进行注解?
-
@vincendep 这完全没有必要。
-
所以在扫描组件时,即使没有注释,存储库也会被注册为 bean?
-
我确实用 @Repository 注释了我的课程。
-
您好,请在您的问题中包含带有@SpringBootApplication 注解的类
标签: java spring spring-boot