I have a repository B which I extend from another repository A.
Repository A:
@Repository
public class RepositoryA implements Repository {
}
Repository B:
@Repository
public class RepositoryB extends RepositoryA implements RepositoryAux{
}
But when I try to inject the repository I am having the following error:
No unique bean of type [Repository] is defined: expected single matching bean but found 2: [RepositoryA , RepositoryB]
I understand that I can solve it using the annotation @Qualifier. But, my question is:
it's right Overwrite the annotation @Repository in the child class ?
Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name
Thank's in advance
- it's right Overwrite the annotation @Repository in the child class ?
There are a few things you should consider:
a. If you intend to use RepositoryA
as a repository, hen you need @Repository
on both.
b. If you intent to use RepositoryA
just to provide basic functionality so RepositoryB
(or other classes) can extend it, then make RepositoryA
abstract and annotate with NoRepositoryBean
c. Alternatively, you could use " composition over inheritance " principle. In short you could inject RepositoryA
into RepositoryB
and create facade or wrapper methods to access the methods on RepositoryB
instance.
- Can I assign a new identifier to the child class? Instead of using annotation @Qualifier, Something like @Id or @Name
I think you mean @Named
from javax.inject.Named
which is basically equivalent to @Qualifier
I think you mean <bean id='...'
which in this case @Qualifier
is the way to do with annotations. See spring's documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.