Java Spring 无法找到 JpaRepository
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23476427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring not able to locate JpaRepository
提问by WeMakeSoftware
Using JavaConfig I have a problem locating the @Repository
Spring beans.
使用 JavaConfig 我在定位@Repository
Spring bean 时遇到问题。
The repository interface is defined like this:
存储库接口定义如下:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
The configuration is defined like this:
配置定义如下:
@Configuration
@ComponentScan("com.example")
@EnableAutoConfiguration
@EnableJpaRepositories("com.example")
public class SampleApplication extends SpringBootServletInitializer {
...
the package structure looks like this:
包结构如下所示:
com.example
configuration
SampleApplication
repository
UserRepository
In the log file I see that the Repository is foundas a candidate for bean definition, but:
在日志文件中,我看到 Repository 被发现作为 bean 定义的候选对象,但是:
ClassPathBeanDefinitionScanner | Ignored because not a concrete top-level class:
Interesting fact
有趣的事实
if I move the SampleApplication class to the com.example
package, everything starts to work.
如果我将 SampleApplication 类移动到com.example
包中,一切都会开始工作。
Any ideas what I'm missing?
任何想法我错过了什么?
采纳答案by Phil Webb
You might need to use Spring Boot's @EntityScan
annotation if your JPA entities are not in a sub-package of com.example.configuration
. I would also recommend that you move your @Configuration
off the SpringBootServletInitializer
and into its own class.
您可能需要使用Spring Boot的@EntityScan
注解,如果你的JPA实体是不是在一个子包com.example.configuration
。我也建议你移动你的@Configuration
离开SpringBootServletInitializer
,并进入自己的类。
If you can move your configuration class up a level you can drop the @ComponentScan
, @EnableJpaRepositories
and @EntityScan
annotations all together (see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-locating-the-main-class)
如果您可以将配置类上移一个级别,则可以将@ComponentScan
,@EnableJpaRepositories
和@EntityScan
注释放在一起(请参阅http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-locating-主类)
If @EntityScan
doesn't fix things perhaps you could provide an example project that we can look at?
如果@EntityScan
不能解决问题,也许您可以提供一个我们可以查看的示例项目?
回答by Gabriel Ruiu
I normally thought that the package you specify in @EnableJpaRepositories traverses the inner packages as well.
我通常认为您在 @EnableJpaRepositories 中指定的包也会遍历内部包。
When in doubt, you could try the typesafe alternative:
如有疑问,您可以尝试使用类型安全的替代方案:
@Configuration
@ComponentScan("com.example")
@EnableAutoConfiguration
@EnableJpaRepositories (basePackageClasses = {UserRepository.class})
public class SampleApplication extends SpringBootServletInitializer {
...
回答by Valerio Vaudi
The problem hear that using @EnableJpaRepositories("com.example")
the your context when start check for com.example
as base package but it don't goes on. In other words the package scan will stop on the com.example
level. For a deeper stanning, you have to do a things like this @EnableJpaRepositories("com.example.**")
. However in this case the Spring data check all the package com.example
and all the sub package. A more correct approch should be write a thik like this @EnableJpaRepositories("com.example.repository")
or @EnableJpaRepositories("com.example.repository.**")
. In the first case you scan for the repository base package in the second case you scan for the repository and all sub package of repository that in my opinion is the correct approch for this kind of case.
问题是@EnableJpaRepositories("com.example")
在开始检查时使用您的上下文com.example
作为基本包,但它没有继续。换句话说,包扫描将在com.example
级别上停止。对于更深的晒黑,你必须做这样的事情@EnableJpaRepositories("com.example.**")
。但是在这种情况下,Spring 数据会检查所有包com.example
和所有子包。更正确的方法应该是像这样写一个 thik@EnableJpaRepositories("com.example.repository")
或@EnableJpaRepositories("com.example.repository.**")
. 在第一种情况下,您扫描存储库基础包,在第二种情况下,您扫描存储库和存储库的所有子包,在我看来,这是这种情况的正确方法。
I hope that this can help you
我希望这可以帮助你
回答by Aravind J
@EnableJpaRepositories (basePackageClasses = {SchedulerRepository.class})
worked for me in my case , though I had added all the annotations to pick up repository
在我的情况下为我工作,尽管我已经添加了所有注释来获取存储库