Affects: Spring Context 5.4.5

When I want to use this method to find those beans annotated with a custom annotation it always return true Did I misunderstand the Java Docs

Determine whether the given class is a candidate for carrying the specified annotation(at type, method or field level).

    public @interface Job{
          String value();
    }
    // customer BeanPostProcessor
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
        if (AnnotationUtils.isCandidateClass(targetClass, Job.class)) {
            //do something 
        }
    }

Comment From: kashike

isCandidateClass does not do what you are thinking - it determines if the given annotation can be applied to a class, not if the class actually has the annotation.

targetClass.isAnnotationPresent(Job.class) might be what you are looking for, and should be enough in most cases.

Comment From: sbrannen

@kashike is correct.

AnnotationUtils.isCandidateClass is an internal utility that does not provide much value outside the framework.

If you are trying to determine if a given class is directly annotated with a particular annotation, then targetClass.isAnnotationPresent(Job.class) will suffice.

However, if you wish to find @Job when it's used as a meta-annotation or whenever you need support for merged annotation attributes, you'd be better off using AnnotatedElementUtils.hasAnnotation(), AnnotatedElementUtils.findMergedAnnotation(), or the MergedAnnotations API directly.