Understanding the Spring Data JPA @NoRepositoryBean interface

The annotation is used to avoid creating repository proxies for interfaces that actually match the criteria of a repo interface but are not intended to be one. It's only required once you start going into extending all repositories with functionality. Let me give you an example:

Assume you'd like to add a method foo() to all of your repositories. You would start by adding a repo interface like this

public interface com.foobar.MyBaseInterface<…,…> extends CrudRepository<…,…> {

  void foo();
}

You would also add the according implementation class, factory and so on. You concrete repository interfaces would now extend that intermediate interface:

public interface com.foobar.CustomerRepository extends MyBaseInterface<Customer, Long> {

}

Now assume you bootstrap - let's say Spring Data JPA - as follows:

<jpa:repositories base-package="com.foobar" />

You use com.foobar because you have CustomerRepository in the same package. The Spring Data infrastructure now has no way to tell that the MyBaseRepository is not a concrete repository interface but rather acts as intermediate repo to expose the additional method. So it would try to create a repository proxy instance for it and fail. You can now use @NoRepositoryBean to annotate this intermediate interface to essentially tell Spring Data: don't create a repository proxy bean for this interface.

That scenario is also the reason why CrudRepository and PagingAndSortingRepository carry this annotation as well. If the package scanning picked those up by accident (because you've accidentally configured it this way) the bootstrap would fail.

Long story short: use the annotation to prevent repository interfaces from being picked up as candidates to end up as repository bean instances eventually.


We can declare a new interface as our custom method:

@NoRepositoryBean
public interface ExtendedRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
    List<T> findByAttributeContainsText(String attributeName, String text);
}

Our interface extends the JpaRepository interface so that we'll benefit from all the standard behavior.

You'll also notice we added the @NoRepositoryBean annotation. This is necessary because otherwise, the default Spring behavior is to create an implementation for all subinterfaces of Repository.

public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
}