Reputation: 5281
I have to implement custom repository class annotated with @Repository
which should inherit another class which is annotated @Repository
as well.
What is correct implementation of that usecase? Can directly inherit that class and add @Repository
to main, or there is another best practice? Actually I have problem when I have defined
@EnableJpaRepositories(basePackages = { "com.example.foo.repositories", "com.example.bar.repositories" }
in @Configuration
class in root it doesn't scan my repositories and I can't autowire it.
here is sample of my repository class:
parent repository (third party class):
@Repository
public abstract class ParentRepository {
// ...
}
interface and impl class which are in package com.example.foo.repositories
:
public interface IFooRepository {
Foo getFoo();
}
@Repository
public class FooRepository extends ParentRepository implements IFooRepository {
Foo getFoo() {
// ...
}
}
Do you have idea how to fix it and make possible to autowire IFooRepository ? Thank you in advice.
EDIT:
I find out green bean next to @EnableJpaRepositories and when I click on the bean it redirects me to bar repository, and doesn't show FooRepository Bean. I don't understand because both repositories are identical implemented.
Upvotes: 0
Views: 768
Reputation: 2221
According to the doc for jpa repositories
In the preceding example, Spring is instructed to scan com.acme.repositories and all its subpackages for interfaces extending Repository or one of its subinterfaces. For each interface found, the infrastructure registers the persistence technology-specific FactoryBean to create the appropriate proxies that handle invocations of the query methods. see
So basicly @EnableJpaRepositories
- is the same as xml configuration from the link - it instructs to find classes that extending Repository
. In you example you have @Repository
annotation - that instructs Spring to translate exceptions. You should includ "com.example.foo.repositories", "com.example.bar.repositories"
into components scan. Try @ComponentScan
annotation see
Upvotes: 1
Reputation: 421
Adding the @Primary
annotation to your custom repository should autowire it into another class whenever possible by default, even if there are other valid beans for the autowire.
Upvotes: 0