1

I have problems autowiring beans which are derived from interfaces with annotations.

@Component
@EnableAsync
public interface Calculator {
  @Async
  public Future<String> calculate();
}

public interface SpecificCalculator extends Calculator {
}

public class ConcreteSpecificCalculator implements SpecificCalculator {

  @Override
  public Future<String> calculate() {
    // do calculation here
    return new AsyncResult<String>("hello");
  }

}

From what I understood from: Annotations on Interfaces? the @Component Annotation should also apply to all subclasses. Now I have a Mapper class returning all types that are derived from SpecificCalculator.

@Component
public class CalculatorMapper {

@Autowired
private List<SpecificCalculator> specificCalculators;

public List<Calculator> retrieveCalculatorsByModuleId(Integer moduleId) {
  if(moduleId==...){
    return specificCalculators;
  }else{
  ...
  }
}

This is not working. Spring does not find the ConcreteSpecificCalculator and is not injecting it. If I Annotate ConcreteSpecificCalculator with @Component again, it is working. Can anybody explain this to me?

Community
  • 1
  • 1
hFonti
  • 187
  • 1
  • 4
  • 11
  • Some discussion here: http://stackoverflow.com/questions/7083308/is-there-a-way-in-spring-to-autowire-all-dependencies-of-a-given-type Do you need the @Configurable(autowire=Autowire.BY_TYPE) at the class level? Not sure... – Alan Hay Oct 20 '16 at 17:23

0 Answers0