Java Programming Tutorials

Java programming tutorials with many code examples!

Spring component scan exclude

Spring Framework

Problem:

How to exclude classes/packages from Component Scan in Spring Framework? The task may be tricky, especially when regexp is involved. See how it works!

Solution:

Let’s say that we’ve got the following two classes in com.farenda.spring.tutorial.excludescan package:

package com.farenda.spring.tutorial.excludescan;

import org.springframework.stereotype.Service;

@Service
public class ExcludedService {

    public ExcludedService() {
        System.out.println("Instantiating " + getClass().getSimpleName());
    }
}
package com.farenda.spring.tutorial.excludescan;

import org.springframework.stereotype.Service;

@Service
public class IncludedService {

    public IncludedService() {
        System.out.println("Instantiating " + getClass().getSimpleName());
    }
}

We want to scan whole package and include every component (only IncludedService in this case) except ExcludedService. To do that we can use excludeFilters on the @ComponentScan annotation:

package com.farenda.spring.tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.ComponentScan.Filter;

import java.util.Arrays;

@ComponentScan(basePackages = "com.farenda.spring.tutorial.excludescan",
        excludeFilters =@Filter(
                type = FilterType.REGEX,
                pattern = "com\\.farenda\\.spring\\.tutorial\\.excludescan\\.Excluded.*"))
@EnableAutoConfiguration
public class ExcludingFromComponentScanExample {

    public static void main(String[] args) throws Exception {
        ApplicationContext context =
                SpringApplication.run(ExcludingFromComponentScanExample.class, args);

        String[] beans = context.getBeanDefinitionNames();

        // sort names only to make output more readable:
        Arrays.sort(beans);

        System.out.println("Defined beans: ");
        for (String bean : beans) {
            System.out.println(bean);
        }
    }
}

We apply REGEX FilterType that allows us nicely exclude single classes and whole packages. When we run the application you can see that only includedService is among beans in ApplicationContext built from Component Scan:

...
 :: Spring Boot ::        (v1.2.3.RELEASE)

2015-12-20 13:08:37.601  INFO 4077 --- [           main] .f.s.t.ExcludingFromComponentScanExample
2015-12-20 13:08:37.660  INFO 4077 --- [           main] s.c.a.AnnotationConfigApplicationContext
Instantiating IncludedService
...
2015-12-20 13:08:39.292  INFO 4077 --- [           main] .f.s.t.ExcludingFromComponentScanExample : Started ExcludingFromComponentScanExample in 2.204 seconds (JVM running for 2.841)
Defined beans:
excludingFromComponentScanExample
includedService
mbeanExporter
mbeanServer
objectNamingStrategy
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.condition.BeanTypeRegistry
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
propertySourcesPlaceholderConfigurer
2015-12-20 13:08:39.295  INFO 4077 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7a187f14: startup date [Sun Dec 20 13:08:37 CET 2015]; root of context hierarchy
2015-12-20 13:08:39.298  INFO 4077 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Process finished with exit code 0

The same filtering mechanism can be used with includeFilters attribute, to select classes/packages that should be included in Spring ApplicationContext.

Share with the World!
  • Harshad

    Nice info!
    If the whole package needs to be excluded then the pattern should be as below:
    pattern = “com\.farenda\.spring\.tutorial\.excludescan\..*”