Keep track of all classes implementing a particular interface?

It is hard to explain what I really want. I have an interface that has a getRuntimeInfo() method that provides me with all the runtime debugging information for class variables. I want to see a list of all classes implementing this interface. I use Java and Spring. One way to do this is to get all the beans from the Spring context and check with the instanceof operator. But I would not want to do this for an obvious impact on performance. Do I have another option?

+8
java spring interface
source share
2 answers

How about this solution:

 @Component public class WithAllMyInterfaceImpls { @Autowire List<MyInterface> allBeansThatImplementTheMyInterface; } 

The list is filled only once (at startup), so it should not have a significant impact on the "normal" performance of execution.


A comment:

can you explain your code

You know that Spring is an IOC container. @Component tells Spring that it must instantiate this class (the so-called Spring Managed Bean)). IOC also means that the Container is responsible for injecting links to other instances (Spring Managed Beans). @Autowire (as well as @Resource and @Inject - all do the same) - this is such an annotation that tells Spring that this field should be populated by Spring. Spring itself tries to figure out with what instances the field should be populated. The default method used by Spring is by type , which means that Spring checks the type of the field and looks for suitable beans. In your case, this is a general list - this is a bit special. In this case, Spring populate the field with a list, where all beans elements are generic.

+13
source share

How to get getBeansOfType method from ApplicationContext? Does it return a beans map that implements your interface?

http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/beans/factory/ListableBeanFactory.html#getBeansOfType(java.lang.Class )

+2
source share

All Articles