Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring not able to locate JpaRepository

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?

like image 427
WeMakeSoftware Avatar asked May 05 '14 15:05

WeMakeSoftware


People also ask

How do I enable JpaRepository in Spring boot?

To activate the Spring JPA repository support, we can use the @EnableJpaRepositories annotation and specify the package that contains the DAO interfaces: @EnableJpaRepositories(basePackages = "com.

Can we use JpaRepository in Spring MVC?

Spring Data JPA provides CRUD API, so you don't have to write boilerplate code. You just need to create a repository interface and spring will provide implementation automatically.


2 Answers

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?

like image 71
Phil Webb Avatar answered Oct 05 '22 04:10

Phil Webb


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

like image 32
Valerio Vaudi Avatar answered Oct 05 '22 03:10

Valerio Vaudi