Spring bean checking for classes

I want to scan all classes in a package that are subclasses of a certain class. Then I want to take these classes and for each one create a bean instance of the same type using the class as a property. Then I want to collect all these anonymous beans and put them in a collection. Is it possible to configure spring context in XML like this? thank

+2


source to share


4 answers


Just use this class:

ClassPathScanningCandidateComponentProvider



method: findCandidateComponents.

      

+2


source


In Java, you cannot enumerate all the classes contained in a package without directly accessing the location where the class files are stored (usually the file system).



For the second part of your question, have a look at spring-list-beans-by-type .

0


source


The closest thing that sounds like you are asking is a template <context:component-scan>

(see the docs here ), but what you're asking for is very specific and honestly doesn't make a lot of sense. If you could give a specific example, that would help a lot.

0


source


Probably the best way to implement this is to write your own implementation of the Spring FactoryBean interface and do all the introspection / reflection and scanning in that implementation to create the end of the bean you want to put in the Spring applicationContext.

0


source







All Articles