Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

AnnotationUtils.findAnnotation returns null

Writer Matthew Martinez

I'm attempting to find all beans with a custom annotation so I can look at the annotation properties on each bean for help in a scheduled job.

Custom Spring Annotation:

@Component
@Scope("prototype")
public @interface Importer { String value() default ""; String fileRegex() default "";
}

Example Class Definition (MyAwesomeImporterFramework is a base class--no annotations)

@Importer(value="spring.bean.name", fileRegex="myfile.*\\.csv")
public class MyAwesomeImporter extends MyAwesomeImporterFramework

Code to find Spring beans with annotations:

public static List<Class<?>> findBeanClasses(String packageName, Class<? extends Annotation> annotation) throws ClassNotFoundException { List<Class<?>> classes = new LinkedList<Class<?>>(); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(new AnnotationTypeFilter(annotation)); for(BeanDefinition def : scanner.findCandidateComponents(packageName)) { classes.add(Class.forName(def.getBeanClassName())); } return classes;
}

Code that utilizes the finder to get the annotation properties.

for(Class<?> clazz : AnnotationFinder.findBeanClasses(CLASS_BASE_PACKAGE, Importer.class)) { // Doesn't work either: // Importer annotation = clazz.getAnnotation(Importer.class); Importer annotation = AnnotationUtils.findAnnotation(clazz, Importer.class); importClasses.put(annotation.fileRegex(), annotation.value());
}

Here, both clazz.getAnnotation(Importer.class) and AnnotationUtils.findAnnotation(clazz, Importer.class) return null. Inspecting the code in the debugger shows the correct classes being identified, but annotations map on clazz is empty.

What am I missing? Both of those methods should return something, but it almost seems like the annotation has disappeared off the class during runtime??

1 Related questions 1 Java Spring Problem with annotations 14 java.lang.NoSuchMethodError org.springframework.core.annotation.AnnotationUtils.getAnnotation 0 Annotated classes can't be found by Spring Related questions 1 Java Spring Problem with annotations 14 java.lang.NoSuchMethodError org.springframework.core.annotation.AnnotationUtils.getAnnotation 0 Annotated classes can't be found by Spring 20 getAnnotations() is empty 0 Reflection can not find annotation 1 getAnnotations() empty 3 getAnnotation(Class) return always null while processing annotations 1 Java - get annotation is always null 5 getAnnotation returns null for multiple annotations 2 getAnnotation returns null for methods in different maven module Load 7 more related questions Show fewer related questions Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.