Spring: general injection does not work

In my project, I have the following repository structure:

public interface IRepository<T> { ... }

public abstract class AbstractRepository<T> implements IRepository<T> { ... }

@Repository
public class ARepository extends AbstractRepository<A>
    implements IRepository<A> { ... }  // added line 


@Repository
public class BRepository extends AbstractRepository<B>
    implements IRepository<B> { ... }  // added line 


@Repository
public class CRepository extends AbstractRepository<C>
    implements IRepository<C> { ... }  // added line 

Now I would like to add them to the appropriate services as follows:

public class MyServiceImpl implements MyService {

    @Autowire
    IRepository<A> arepository;

    ...
}

It compiles fine, but when I start the Spring context, I get the following error No qualifying bean of type 'some.package.IRepository<?>' available: expected single matching bean but found 3: aRepository, bRepository, cRepository.

As far as I know, injection in Spring 4 (4.3.13.RELEASE used in my project) should handle this situation well, using generic types as a form of qualifier. Unfortunately, this does not work for me. Should I change some configurations? I searched a bit for this topic, but did not find anything useful. I ended up using names for my beans annotation and @Qualifier, but with this solution, I'm still implementation dependent.

Has anyone had similar problems and managed to solve them?

@Edit , , - , , (CTW) ajc.

+4
1

:

public interface MyInterface<T> {
}

public abstract class MyAbstract<T> implements MyInterface<T> {
}

@Service
public class MyServiceInteger extends MyAbstract<Integer> implements MyInterface<Integer> {
}

@Service
public class MyServiceString extends MyAbstract<String> implements MyInterface<String> {
}

//in a service
@Inject
private MyInterface<String> stringSomething;

. - , , , (String). 100% , () Spring 4.x? .

0

Source: https://habr.com/ru/post/1692702/


All Articles