Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 978 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding the Spring Data JPA @NoRepositoryBean interface

#1
I encountered the `@NoRepositoryBean` interface several times whilst reading the Spring Data documentation.

To quote from the documentation:
> If you're using automatic repository interface detection using the
> Spring namespace using the interface just as is will cause Spring
> trying to create an instance of MyRepository. This is of course not
> desired as it just acts as indermediate between Repository and the
> actual repository interfaces you want to define for each entity. To
> exclude an interface extending Repository from being instantiated as
> repository instance annotate it with `@NoRepositoryBean`.

However, I am still not sure when and where to use it. Can someone please advise and give me a concrete usage example?
Reply

#2
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.
Reply

#3
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> {
}
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through