mercredi 21 août 2019

Scanning Java classes in main project from library

I've creted a Java Library that uses reflection to scan classes based on a filter (base package name). This library only contains the scanning logic, because the idea is to import it into several projects that have the same structure (package structure). I'm using ClassPathScanningCandidateComponentProvider, this is the implementation:

ClassPathScanningCandidateComponentProvider classScanner = new ClassPathScanningCandidateComponentProvider(false);
classScanner.addIncludeFilter(new AnnotationTypeFilter(ProductApi.class));
List<PathsMapper> pathsMappers = new ArrayList();
Iterator var3 = classScanner.findCandidateComponents("com.api.v3.apis").iterator();

I've tested this code within a project and it works perfectly, but if I extract it to a library and then import it to any of the project, it doesn't scan the classes, I don't receive any error message or similar.

How can I specify that the scan must be in the project that imported the library? Is there any other approach better than this?





Aucun commentaire:

Enregistrer un commentaire