I have a web application built with Spring and running inside Jboss. I am trying, at runtime, to scan for classes that have a certain annotation. Right now I am using the open source Reflections library
public static List<Class> scanForClassesWithAnnotation(Class<? extends Annotation> annotationClass)
{
List<Class> classesWithAnnotation = new ArrayList<>();
for(Package p : Package.getPackages())
{
LOGGER.info("scanForClassesWithAnnotation: Examining package " + p.getName());
try
{
List<ClassLoader> classLoadersList = new ArrayList<>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[classLoadersList.size()])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(p.getName()))));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
LOGGER.info("\tscanForClassesWithAnnotation: found classes " + JsonUtils.objectToJson(classes));
for(Class foundClass : classes)
{
if(foundClass.isAnnotationPresent(annotationClass))
{
classesWithAnnotation.add(foundClass);
}
}
} catch (Exception ignored) {}
}
return classesWithAnnotation;
}
When this code executes I see many of these such messages in the log:
given scan urls are empty. set urls in the configuration
This code works in a standalone Maven project, so I know it works in general. But I think the problem is with the fact that the code is running in a servlet context. How do I fix this? How do I scan for classes with an annotation in a Spring webapp? Thanks in advance.