我在与配置类不同的包中有一个存储库,因此我使用@Repostiory 将其注释为以下内容:
package test;
@Repository
public interface UserTest extends JpaRepository<User, Long> {
}
我已经对其进行了组件扫描,但它不起作用:
package com.app;
@SpringBootApplication
@ComponentScan({"test","com.app"})
public class Application extends SpringBootServletInitializer {
}
例外:没有可用的“test.UserTest”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。
除非我添加 enableJpaRepositories ,否则为什么组件扫描不能在存储库上工作?我认为 componetScan 就足够了
更新:
由于一些答案提供了解决方案,我问的是解释而不是解决方案。以下内容甚至无需对“test”进行组件扫描即可工作:
SpringBootApplication
@EnableJpaRepositories({"test","com.app"})
public class Application extends SpringBootServletInitializer{
}
现在的问题是为什么当它不起作用时我什至需要在 @Repository 上使用组件扫描?为什么在文档中 @Repository 被组件扫描扫描时它没有效果并且@EnableJpaRepostiories 是enoguh?
来自关于组件扫描的 Spring 文档:指示是否应启用对使用 @Component @Repository、@Service 或 @Controller 注释的类的自动检测。
在我的情况下,@Repository 未被检测到