awk
awk

Reputation: 526

spring component scan for classes

I want to scan all the classes in a package that that are subclasses of the particular class. Then I want to take these classes and for each of them instantiate a bean of same type, using the class as a property. Then I want to gather all these anonymous beans and put them into collection. Is it possible to configure spring context in XML like this? Thx

Upvotes: 3

Views: 4697

Answers (4)

Carlos Lopez
Carlos Lopez

Reputation: 94

Just use this class:

ClassPathScanningCandidateComponentProvider

method: findCandidateComponents.

Upvotes: 2

Alex Marshall
Alex Marshall

Reputation: 10312

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

Upvotes: 0

skaffman
skaffman

Reputation: 403471

The closest thing that sounds like what you're asking for is the <context:component-scan> pattern (see docs here), but what you're asking for is highly specific, and to be honest, doesn't make much sense. If you could give a concrete example, it would help a lot.

Upvotes: 0

tangens
tangens

Reputation: 39733

In Java you can't list all classes that are contained in a package without direct access to the location where the class files are stored (usually the filesystem).

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

Upvotes: 0

Related Questions