0

Below are the classes and the error mentioned. Spring version: 5+

public interface CustomGenerator<T,R> {
  void generate();
}
@Service
public abstract class AbstractGenerator<T extends CustomClassA, R extends CustomClassB>
implements CustomGenerator<T,R> {}
@Service
public class ConcreteC1 extends AbstractGenerator<CustomClassA, CustomClassB>{

 void generate(){
  ...
 }
}
@Service
public class ConcreteC2 extends AbstractGenerator<CustomClassA, CustomClassB>{

 void generate(){
  ...
 }
}
@Service
public class CustomService {

@Autowired
private List<CustomGenerator<CustomClassA, CustomClassB>> customGenerators;
// Expected: ConcreteC1 & ConcreteC1 instances in the list

}

Error: org.springframework.beans.factory.UnsatisfiedDependencyException Unsatisfied dependency expressed through field 'customGenerators' nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type.

I want a list of concrete classes instead of utilizing qualifiers to pick one by one, as I want to execute the generate method for all the elements in the list at once. Thanks for any help in advance.

aayoustic
  • 99
  • 9
  • Perhaps you’ll find this question useful: https://stackoverflow.com/questions/50986896/spring-autowiring-generic-implementation-of-generic-interface – justis Jun 08 '22 at 17:17

3 Answers3

0

Rewrite the definition of List

@Service
public class CustomService {
    @Autowired
    private List<? extends CustomGenerator<? extends CustomClassA, ? extends CustomClassB>> customGenerators;
}

Spring will help to inject the list with ConcreteX bean.

shanfeng
  • 503
  • 2
  • 14
  • Definitely, but then it would defeat the purpose of multiple implementations with a restriction provided at the abstracted layer. I understand my example doesn't have CustomClassC & D which would extend CustomClassA & B and be used as the value for the generic types for the concrete classes, but that is understood. It isn't easy, but using in order to use it vs using to solve something matters :) – aayoustic Jun 09 '22 at 10:21
  • @AayushShrivastava We can try to rewrite type of list to solve it. – shanfeng Jun 10 '22 at 01:49
-1

Tested this example as exactly as I could with version 5.3.2 and it seems to work for me.

Perhaps your problem is something different? Some follow up questions that might help getting to the bottom of it:

  • Are all the classes correctly configured in the component scan, for example?
  • Exact spring version info
dntk
  • 41
  • 5
-1

The below fix while autowiring made this silly mistake go away. Thanks, everyone for your time & responses :)

@Service
public class CustomService {

@Autowired
private List<CustomGenerator<? extends CustomClassA, ? extends CustomClassB>> customGenerators;

}
aayoustic
  • 99
  • 9