Use classpathscanningandidatecomponentprovider with multiple jar files?

I am looking to use classpathscanningcandidatecomponentprovider to find subclasses of specific classes in my JVM

What I am doing is described here: scanning Java annotations at runtime

However, when I called the code from ant, I encountered a serious problem through JMX beans

I use the search package to call: classpathscanningcandidatecomponentprovider findCandidateComponents:“com.mycompany”

However, there are multiple jar files in my classpath that contain classes starting with the package Spring stops scanning after scanning the first one (I know it's like searching sublasees of java.lang.object. I get all classes in a jar file)

Is there any way to tell classpathscanningcandidatecomponentprovider / spring not to stop scanning after the first jar?

Cheers!

Solution

It turns out that I have to define classloader explicitly, because when running a scan from JMX, it will look for classes on different loaders and find nothing

//Add that at top of class
private static final ClassLoader classLoader = MyClass.class.getClassLoader();

...

ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider( true);

provider.addIncludeFilter(new AssignableTypeFilter(forClass));

//Had to add this line
provider.setResourceLoader(new PathMatchingResourcePatternResolver(classLoader));

final Set<BeanDeFinition> candidates = provider.findCandidateComponents(SEARCH_PACKAGE);

...
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>