Spring has something called a AnnotatedTypeScanner
class.
This class internally uses
ClassPathScanningCandidateComponentProvider
This class has the code for actual scanning of the classpath resources. It does this by using the class metadata available at runtime.
One can simply extend this class or use the same class for scanning. Below is the constructor definition.
/**
* Creates a new {@link AnnotatedTypeScanner} for the given annotation types.
*
* @param considerInterfaces whether to consider interfaces as well.
* @param annotationTypes the annotations to scan for.
*/
public AnnotatedTypeScanner(boolean considerInterfaces, Class extends Annotation>... annotationTypes) {
this.annotationTypess = Arrays.asList(annotationTypes);
this.considerInterfaces = considerInterfaces;
}