Bird
Raised Fist0
Spring Bootframework~7 mins

Custom auto-configuration in Spring Boot

Choose your learning style10 modes available
Introduction

Custom auto-configuration helps your Spring Boot app set up beans automatically without manual setup. It saves time and keeps your code clean.

You want to provide reusable setup for your own library or module.
You want to add default beans that users can override easily.
You want to simplify configuration for common use cases.
You want to conditionally create beans based on classpath or properties.
You want to share configuration across multiple projects.
Syntax
Spring Boot
@Configuration
@ConditionalOnClass(SomeClass.class)
@ConditionalOnProperty(prefix = "my.feature", name = "enabled", havingValue = "true", matchIfMissing = true)
public class MyAutoConfiguration {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}

@Configuration marks the class as a source of bean definitions.

@ConditionalOnClass creates beans only if a class is on the classpath.

Examples
This auto-configuration creates a DataSource bean only if the DataSource class is available.
Spring Boot
@Configuration
@ConditionalOnClass(DataSource.class)
public class DataSourceAutoConfiguration {

    @Bean
    public DataSource dataSource() {
        return new HikariDataSource();
    }
}
This bean is created only if the property feature.enabled=true is set.
Spring Boot
@Configuration
@ConditionalOnProperty(prefix = "feature", name = "enabled", havingValue = "true")
public class FeatureAutoConfiguration {

    @Bean
    public FeatureService featureService() {
        return new FeatureService();
    }
}
This creates a MyService bean only if no other MyService bean is defined.
Spring Boot
@Configuration
@ConditionalOnMissingBean(MyService.class)
public class MyServiceAutoConfiguration {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}
Sample Program

This example shows a custom auto-configuration class that creates a MyService bean if the class is present and a property myapp.service.enabled is true or missing. The main app fetches the bean and prints a greeting.

Spring Boot
package com.example.autoconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

@Configuration
@ConditionalOnClass(MyService.class)
@ConditionalOnProperty(prefix = "myapp.service", name = "enabled", havingValue = "true", matchIfMissing = true)
public class MyServiceAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MyService myService() {
        return new MyService();
    }
}

// Simple service class
class MyService {
    public String greet() {
        return "Hello from MyService!";
    }
}

// Main application to test auto-configuration
package com.example.demo;

import com.example.autoconfig.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
        MyService service = ctx.getBean(MyService.class);
        System.out.println(service.greet());
    }
}
OutputSuccess
Important Notes

Always use @ConditionalOnMissingBean to allow users to override your beans.

Put your auto-configuration classes in spring.factories or use the new spring.autoconfigure file for Spring Boot 3+.

Test your auto-configuration by enabling/disabling properties and checking bean presence.

Summary

Custom auto-configuration creates beans automatically based on conditions.

Use conditions like class presence and properties to control bean creation.

It helps make reusable, easy-to-configure Spring Boot modules.

Practice

(1/5)
1. What is the main purpose of custom auto-configuration in Spring Boot?
easy
A. To automatically create beans based on specific conditions
B. To manually register beans in the application context
C. To disable all default Spring Boot configurations
D. To write custom SQL queries for database access

Solution

  1. Step 1: Understand auto-configuration concept

    Custom auto-configuration automatically creates beans when certain conditions are met, reducing manual setup.
  2. Step 2: Compare options with purpose

    Only To automatically create beans based on specific conditions describes automatic bean creation based on conditions, which matches the purpose.
  3. Final Answer:

    To automatically create beans based on specific conditions -> Option A
  4. Quick Check:

    Auto-configuration = automatic bean creation [OK]
Hint: Auto-configuration means beans created automatically [OK]
Common Mistakes:
  • Confusing auto-configuration with manual bean registration
  • Thinking it disables default configurations
  • Mixing it with database query writing
2. Which annotation is commonly used to mark a class as a custom auto-configuration in Spring Boot?
easy
A. @Configuration
B. @Component
C. @EnableAutoConfiguration
D. @SpringBootApplication

Solution

  1. Step 1: Identify annotation for configuration classes

    @Configuration marks a class as a source of bean definitions, suitable for auto-configuration classes.
  2. Step 2: Exclude other annotations

    @Component is generic, @EnableAutoConfiguration is used on main app class, @SpringBootApplication is a composite annotation.
  3. Final Answer:

    @Configuration -> Option A
  4. Quick Check:

    Auto-config class needs @Configuration [OK]
Hint: Auto-config classes use @Configuration annotation [OK]
Common Mistakes:
  • Using @Component instead of @Configuration
  • Confusing @EnableAutoConfiguration with auto-config class annotation
  • Using @SpringBootApplication on config classes
3. Given this custom auto-configuration snippet, what bean will be created if com.example.MyService is on the classpath and property my.feature.enabled=true?
@Configuration
@ConditionalOnClass(name = "com.example.MyService")
@ConditionalOnProperty(prefix = "my.feature", name = "enabled", havingValue = "true")
public class MyFeatureAutoConfiguration {
    @Bean
    public String featureBean() {
        return "Feature Enabled";
    }
}
medium
A. No bean will be created
B. A bean named featureBean with value "Feature Enabled" will be created
C. A bean named myService will be created
D. An exception will be thrown at startup

Solution

  1. Step 1: Check conditions for bean creation

    Bean is created only if class com.example.MyService exists and property my.feature.enabled=true.
  2. Step 2: Given conditions are met

    Since both conditions are true, the featureBean method creates a bean with value "Feature Enabled".
  3. Final Answer:

    A bean named featureBean with value "Feature Enabled" will be created -> Option B
  4. Quick Check:

    Conditions met = bean created [OK]
Hint: Bean created only if class and property conditions are true [OK]
Common Mistakes:
  • Ignoring @ConditionalOnProperty and assuming bean always created
  • Confusing bean name with class name
  • Expecting exceptions when conditions are met
4. Identify the error in this custom auto-configuration class:
@Configuration
@ConditionalOnClass(name = "com.example.Service")
public class ServiceAutoConfiguration {
    @Bean
    public Service service() {
        return new Service();
    }

    @Bean
    public Service service() {
        return new Service();
    }
}
medium
A. Missing @ConditionalOnProperty annotation
B. No error, code is valid
C. Service class not imported
D. Duplicate bean method names causing a compilation error

Solution

  1. Step 1: Check bean method definitions

    Two methods named service() exist, both annotated with @Bean, causing duplicate bean names.
  2. Step 2: Understand Spring bean naming rules

    Bean names must be unique; duplicate method names cause compilation or context startup errors.
  3. Final Answer:

    Duplicate bean method names causing a compilation error -> Option D
  4. Quick Check:

    Duplicate @Bean methods = error [OK]
Hint: Bean method names must be unique in config classes [OK]
Common Mistakes:
  • Ignoring duplicate method names
  • Thinking missing @ConditionalOnProperty is an error here
  • Assuming missing import causes this error
5. You want to create a custom auto-configuration that only creates a bean if no other bean of the same type exists and a property app.feature.enabled is true. Which combination of annotations should you use on your configuration class and bean method? A) @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method B) @Component on class, @ConditionalOnBean on bean method C) @Configuration on class, @ConditionalOnClass on bean method D) @EnableAutoConfiguration on class, no conditions on bean method
hard
A. @Configuration on class, @ConditionalOnClass on bean method
B. @Component on class, @ConditionalOnBean on bean method
C. @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method
D. @EnableAutoConfiguration on class, no conditions on bean method

Solution

  1. Step 1: Identify class-level annotation for config

    @Configuration is correct to mark a class as configuration for auto-configuration.
  2. Step 2: Choose bean method conditions

    @ConditionalOnMissingBean ensures no existing bean of the type exists; @ConditionalOnProperty checks the property is true.
  3. Step 3: Exclude incorrect options

    @Component is generic, @ConditionalOnBean requires existing bean, @ConditionalOnClass checks class presence, not bean existence, @EnableAutoConfiguration is for main app class.
  4. Final Answer:

    @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method -> Option C
  5. Quick Check:

    Missing bean + property conditions = @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method [OK]
Hint: Use @ConditionalOnMissingBean and @ConditionalOnProperty together [OK]
Common Mistakes:
  • Using @ConditionalOnBean instead of @ConditionalOnMissingBean
  • Putting conditions on class instead of bean method
  • Confusing @EnableAutoConfiguration with config class annotation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard