开发者

spring component scan for classes

开发者 https://www.devze.com 2022-12-08 02:22 出处:网络
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, u

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


Just use this class:

ClassPathScanningCandidateComponentProvider

method: findCandidateComponents.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消