Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Spring find annotated classes


How to find annotated classes using Spring and read metadata from them? Sometimes you may want to attach metadata to your classes using custom annotations. Here’s an example how you can leverage Spring‘s classpath scanning mechanism to do that.


If you use Spring annotations like @Component, @Repository or @Service, then Spring will find such classes, but will make them Spring beans.

Good news is that Spring classpath scanning mechanism is configurable and available in any Spring application. To use custom annotations we have to create an instance of ClassPathScanningCandidateComponentProvider and set appropriate filter – here its AnnotationTypeFilter. It returns BeanDefinitions that contains found class names from which we can get detailed information. The following example makes it clear.

We create our custom annotation that allows us to attach some metatdata:


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// Make the annotation available at runtime:
// Allow to use only on types:
public @interface Findable {

     * User friendly name of annotated class.
    String name();

Sample classes annotated with the custom annotation:


@Findable(name = "Find me")
public class FirstAnnotatedClass {

@Findable(name = "Find me too")
public class SecondAnnotatedClass {

The Java code below is using ClassPathScanningCandidateComponentProvider to scan classes in package. The class is provided by spring-context project. Here’s relevant Maven dependency:


And the code:


import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;

public class SpringClassScanner {

    public static void main(String[] args) throws Exception {
        System.out.println("Finding annotated classes using Spring:");
        new SpringClassScanner().findAnnotatedClasses("");

    public void findAnnotatedClasses(String scanPackage) {
        ClassPathScanningCandidateComponentProvider provider = createComponentScanner();
        for (BeanDefinition beanDef : provider.findCandidateComponents(scanPackage)) {

    private ClassPathScanningCandidateComponentProvider createComponentScanner() {
        // Don't pull default filters (@Component, etc.):
        ClassPathScanningCandidateComponentProvider provider
                = new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(new AnnotationTypeFilter(Findable.class));
        return provider;

    private void printMetadata(BeanDefinition beanDef) {
        try {
            Class<?> cl = Class.forName(beanDef.getBeanClassName());
            Findable findable = cl.getAnnotation(Findable.class);
            System.out.printf("Found class: %s, with meta name: %s%n",
        } catch (Exception e) {
            System.err.println("Got exception: " + e.getMessage());

The code is straightforward. The hardest thing is to type and read very long names of Spring classes. ;-)

And here’s the output of running the code:

Finding annotated classes using Spring:
Found class: SecondAnnotatedClass, with meta name: Find me too
Found class: FirstAnnotatedClass, with meta name: Find me

This sort of scanning is very good fit for applications that already use Spring Framework. In the next posts we’ll see solutions for projects that don’t use Spring.

Share with the World!