Reputation: 9162
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?
Upvotes: 9
Views: 20696
Reputation: 1
@EnableJpaRepositories (basePackageClasses = {SchedulerRepository.class})
worked for me in my case , though I had added all the annotations to pick up repository
Upvotes: 0
Reputation: 4542
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
Upvotes: 4
Reputation: 8642
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?
Upvotes: 16
Reputation: 2803
I normally thought that the package you specify in @EnableJpaRepositories traverses the inner packages as well.
When in doubt, you could try the typesafe alternative:
@Configuration
@ComponentScan("com.example")
@EnableAutoConfiguration
@EnableJpaRepositories (basePackageClasses = {UserRepository.class})
public class SampleApplication extends SpringBootServletInitializer {
...
Upvotes: 0