Spring not able to locate JpaRepository
Solution 1
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.
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)
If @EntityScan
doesn't fix things perhaps you could provide an example project that we can look at?
Solution 2
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.
I hope that this can help you
Comments
-
WeMakeSoftware almost 2 years
Using JavaConfig I have a problem locating the
@Repository
Spring beans.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 found as a candidate for bean definition, but:
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.Any ideas what I'm missing?
-
WeMakeSoftware about 10 yearsTried that with no success. The problem is that it finds the UserRepository as a bean candidate, but later it is being rejected. Furthermore, if I put the SampleApplication class in the com.example package, everything works out of the box
-
Gabriel Ruiu about 10 yearsOne question though: Why are you using the SpringBootServletInitializer as the main class?
-
WeMakeSoftware about 10 yearsI'm deploying to Tomcat, but I need an option to run the app in standalone mode for testing
-
WeMakeSoftware about 10 yearsThanks for the help, @EntityScan("com.example") did the job.
-
norbertas.gaulia almost 9 yearsI have multi-project setup where entities are on a separate project and repositories on others, @EntityScan is the only trick that helped, thanks