Retrieve an ordered list of services from the spring context

2020-02-26 java spring

Given a service interface:

public interface MyService {
  void doSomething();
}

And two implementations:

@Order(value = 10)
@Service
public class MyServiceA implements MyService {
   ...
}

@Order(value = 20)
@Service
public class MyServiceB implements MyService {
   ...
}

Then I can inject a list of all MyService implementation in the order that they are defined through the Order value.

@Autowire
private List<MyService> allMyServices;

How can I achieve the same thing by retrieving the services from the ApplicationContext?

The only thing that I have seen is context.getBeansOfType(MyService.class) which results in a map of the bean names to the beans, but the order is lost.

Answers

Related