Reputation: 1013
I want to get all the implementations of an Interface. One of the answer given here suggests to use Spring DI. It is simple and works.
Please find the sample app here . There is a library module which defines an Interface and has 2 implementations. In application module which is dependent on library, also has additional implementations of same Interface. In the example, the library class just checks the type of a given string(empty, palindrome or text). Obviously if the order is not maintained, the function may return invalid results. Answers to above questions will help me to resolve this problem.
Upvotes: 1
Views: 606
Reputation: 10075
First: You don‘t need reflection. Autowiring is enough:
@Autowired
private List<MyInterface> interfaces;
Second: The list is ordered, but all implementations have the same default order. To solve this you have the annotation @Order that you can place on your classes or implement the Ordered interface.
@Order(1)
@Service
public class MyImpl implements MyInterface {}
You can find this tutorial that explains it a bit more: https://www.baeldung.com/spring-order
Upvotes: 2