Spring Boot
Configuration Exclusion
Java Development
Dependency Management
Software Engineering

springboot How to exclude configuration class in dependency dependency

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Spring Boot dependencies often bring configuration classes that are useful by default but not always appropriate for every application. Excluding the wrong configuration can cause startup failures, while failing to exclude conflicting configuration can produce ambiguous bean definitions. A structured approach helps you disable only what you intend.

Understand the Type of Configuration You Are Excluding

There are two common cases:

  • Auto-configuration classes discovered by Spring Boot.
  • Regular @Configuration classes discovered by component scanning.

The exclusion mechanism depends on which type you are dealing with.

Excluding Auto-Configuration Classes

For Spring Boot auto-configuration, use exclude on your application annotation or properties-based exclusion.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
4
5@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
6public class DemoApplication {
7    public static void main(String[] args) {
8        SpringApplication.run(DemoApplication.class, args);
9    }
10}

Properties-based option:

properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

This is useful when exclusion differs by environment profile.

Excluding Dependency @Configuration via Component Scan Filters

If the dependency exposes regular configuration classes, annotation exclude may not apply. Use component scan filters or explicit import control.

java
1import org.springframework.boot.autoconfigure.SpringBootApplication;
2import org.springframework.context.annotation.ComponentScan;
3import org.springframework.context.annotation.FilterType;
4
5@SpringBootApplication
6@ComponentScan(
7    basePackages = "com.example",
8    excludeFilters = @ComponentScan.Filter(
9        type = FilterType.ASSIGNABLE_TYPE,
10        classes = com.vendor.lib.VendorConfig.class
11    )
12)
13public class DemoApplication {
14}

This prevents a specific dependency config class from entering the context.

Conditional Activation Instead of Hard Exclusion

When you control the dependency code, prefer conditional annotations over hard exclusions. For example, activate configuration only when a property is enabled.

java
1import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4
5@Configuration
6@ConditionalOnProperty(name = "vendor.feature.enabled", havingValue = "true", matchIfMissing = true)
7public class VendorFeatureConfig {
8
9    @Bean
10    public String vendorFeatureMarker() {
11        return "enabled";
12    }
13}

Then disable in your app with a property value. This is safer for library reuse across multiple services.

Diagnosing What Was Loaded

Use startup reports and condition evaluation logs to confirm what configuration was applied.

properties
logging.level.org.springframework.boot.autoconfigure=DEBUG

The condition report clearly shows which auto-configurations matched or were skipped, which is essential when multiple exclusions interact.

For regular configuration classes, list bean definitions or inspect application context at startup in test profiles.

Design Guidance for Large Systems

In multi-service platforms, centralize exclusion strategy in a starter module or shared documentation. Ad hoc exclusions copied across services can drift and create inconsistent behavior.

Treat exclusions as architecture decisions. Record why each exclusion exists and what alternative bean wiring is expected.

Before excluding, verify whether customizing an existing bean is enough. Full exclusion may disable useful supporting beans unintentionally.

Verification with Targeted Tests

Create focused application context tests to verify exclusion behavior explicitly. This prevents accidental reintroduction when dependencies are upgraded.

java
1import org.junit.jupiter.api.Test;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.boot.test.context.SpringBootTest;
4import org.springframework.context.ApplicationContext;
5
6import static org.assertj.core.api.Assertions.assertThat;
7
8@SpringBootTest
9class ConfigExclusionTest {
10
11    @Autowired
12    ApplicationContext context;
13
14    @Test
15    void vendorConfigBeanShouldNotExist() {
16        assertThat(context.containsBean("vendorConfigBean")).isFalse();
17    }
18}

Context tests turn configuration intent into enforceable checks and are especially valuable in large monorepos where dependency upgrades happen frequently. They also provide fast feedback when starter libraries change transitive configuration behavior.

Common Pitfalls

A common pitfall is trying to exclude a regular @Configuration class using auto-configuration exclusion properties. That does nothing and can mislead debugging.

Another issue is broad component scan filters that remove too many dependency beans, causing hidden runtime failures later.

Developers also exclude configuration without adding replacement beans. Startup may pass in one profile but fail in another where the missing bean is actually required.

Finally, teams skip condition report analysis and guess at causes. Always confirm effective configuration using debug logs or integration tests in production pipelines consistently always now.

Summary

  • Identify whether the target is auto-configuration or regular configuration.
  • Use exclude or spring.autoconfigure.exclude for auto-configuration.
  • Use scan filters or explicit imports for regular dependency configuration classes.
  • Prefer conditional configuration when you control dependency code.
  • Validate exclusions with condition reports and integration tests.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.