2
@Component
public class IServiceCollection {
  @Resource
  private IService service1;
  @Resource
  private IService service2;
  @Resource
  private IService service3;
  @Resource
  private IService service4;
  @Resource
  private IService service5;

  public List<IService> getAllServices(){
    List<IService>  iServiceList =  new ArrayList<IService>();

    iServiceList.add(service1);
    iServiceList.add(service2);
    return iServiceList;
  }
}

in IServiceCollection I will refer lots of IService beans like service1, servvice2, etc. I wanna get all of the service beans in method getAllServices().

How can I add all the services to the list automatically, not like the code above?

manish
  • 19,695
  • 5
  • 67
  • 91
how8586
  • 119
  • 1
  • 1
  • 5

1 Answers1

1

You have a few options:

.1. If you inject in a map this way:

@Component
public class IServiceCollection {
  @Autowired
  private Map<String, IService> services;

that would inject in all implementations of IService with the key of the map being the bean name

.2. You can inject in a list this way:

@Component
public class IServiceCollection {

  @Autowired
  private List<IService> services;

again you would have a list of IService instances.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125