Bird
Raised Fist0
Spring Bootframework~30 mins

How auto-configuration works in Spring Boot - Try It Yourself

Choose your learning style10 modes available
How auto-configuration works
📖 Scenario: You are building a simple Spring Boot application that automatically configures a greeting service based on the presence of a property.
🎯 Goal: Learn how Spring Boot auto-configuration works by creating a configuration class that sets up a greeting service bean automatically when a property is set.
📋 What You'll Learn
Create a Spring Boot application class
Create a configuration class with @Configuration and @ConditionalOnProperty
Define a bean method that returns a greeting message
Use the auto-configured bean in the main application
💡 Why This Matters
🌍 Real World
Auto-configuration helps developers avoid writing boilerplate setup code by letting Spring Boot create beans automatically based on conditions like properties or classpath.
💼 Career
Understanding auto-configuration is essential for Spring Boot developers to customize and extend applications efficiently without manual wiring.
Progress0 / 4 steps
1
Create the Spring Boot application class
Create a class called AutoConfigApplication annotated with @SpringBootApplication and add a main method that runs SpringApplication.run(AutoConfigApplication.class, args).
Spring Boot
Hint

This is the standard way to start a Spring Boot app. Use @SpringBootApplication on the class and a main method to launch.

2
Create a configuration class with conditional property
Create a class called GreetingAutoConfiguration annotated with @Configuration and @ConditionalOnProperty(name = "greeting.enabled", havingValue = "true").
Spring Boot
Hint

Use @Configuration to mark the class as config. Use @ConditionalOnProperty to activate only when greeting.enabled=true.

3
Define a greeting bean method
Inside GreetingAutoConfiguration, add a method called greetingMessage annotated with @Bean that returns the string "Hello from auto-configured bean!".
Spring Boot
Hint

Use @Bean on a method that returns the greeting string. This creates a bean in the app context.

4
Use the auto-configured greeting bean
In AutoConfigApplication, add a @Bean method called runner that takes a String greetingMessage parameter and returns a CommandLineRunner which prints the greeting message.
Spring Boot
Hint

Use a CommandLineRunner bean to run code at startup. Inject the greetingMessage bean and print it.

Practice

(1/5)
1. What is the main purpose of Spring Boot's auto-configuration feature?
easy
A. To disable all default Spring features
B. To require manual configuration of all beans and settings
C. To force the use of XML configuration files
D. To automatically set up common configurations based on project dependencies

Solution

  1. Step 1: Understand auto-configuration role

    Auto-configuration helps by setting up default configurations automatically when certain dependencies are present.
  2. Step 2: Compare options

    Options A, B, and D contradict the purpose of auto-configuration, which is to reduce manual setup, not increase it or disable features.
  3. Final Answer:

    To automatically set up common configurations based on project dependencies -> Option D
  4. Quick Check:

    Auto-configuration = automatic setup [OK]
Hint: Auto-configuration means less manual setup [OK]
Common Mistakes:
  • Thinking auto-configuration disables features
  • Assuming all configuration must be manual
  • Confusing auto-configuration with XML config
2. Which annotation enables Spring Boot's auto-configuration in a main application class?
easy
A. @SpringBootApplication
B. @EnableAutoConfig
C. @AutoConfigEnabled
D. @ConfigurationAuto

Solution

  1. Step 1: Identify the correct annotation

    @SpringBootApplication is a meta-annotation that includes @EnableAutoConfiguration, enabling auto-configuration.
  2. Step 2: Verify other options

    Options B, C, and D are not valid Spring Boot annotations and will cause errors.
  3. Final Answer:

    @SpringBootApplication -> Option A
  4. Quick Check:

    @SpringBootApplication enables auto-config [OK]
Hint: Use @SpringBootApplication for auto-config [OK]
Common Mistakes:
  • Using non-existent annotations
  • Forgetting @SpringBootApplication on main class
  • Confusing with @Configuration only
3. Given this Spring Boot application snippet, what will happen if spring-boot-starter-web is on the classpath?
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}
medium
A. Spring Boot auto-configures a web server and MVC setup
B. No web server is configured automatically
C. Application fails to start due to missing annotations
D. Only a command-line runner is configured

Solution

  1. Step 1: Check classpath dependency effect

    Presence of spring-boot-starter-web triggers auto-configuration of embedded web server and MVC components.
  2. Step 2: Analyze application code

    The main class is annotated with @SpringBootApplication, enabling auto-configuration which activates for web components due to the starter-web dependency.
  3. Final Answer:

    Spring Boot auto-configures a web server and MVC setup -> Option A
  4. Quick Check:

    Web starter on classpath = web auto-config [OK]
Hint: Web starter triggers web server auto-config [OK]
Common Mistakes:
  • Ignoring the effect of dependencies on auto-config
  • Assuming no server without explicit config
  • Forgetting @SpringBootApplication annotation
4. What is the likely cause of this error in a Spring Boot app?
@SpringBootApplication
public class App {
  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }
}

// Error: No qualifying bean of type 'DataSource' available
medium
A. Incorrect main method signature
B. Auto-configuration is disabled by default
C. Missing database driver dependency causing auto-config to fail
D. Spring Boot does not support DataSource auto-configuration

Solution

  1. Step 1: Understand DataSource error

    Spring Boot tries to auto-configure a DataSource but fails if no driver dependency is found.
  2. Step 2: Check other options

    Main method is correct, auto-configuration is enabled by default, and Spring Boot supports DataSource auto-config.
  3. Final Answer:

    Missing database driver dependency causing auto-config to fail -> Option C
  4. Quick Check:

    Missing driver = DataSource bean error [OK]
Hint: Add DB driver to fix DataSource bean error [OK]
Common Mistakes:
  • Assuming main method is wrong
  • Thinking auto-config is off by default
  • Believing Spring Boot lacks DataSource support
5. You want to disable Spring Boot's auto-configuration for JPA but keep others enabled. Which is the correct way to do this?
hard
A. Remove all dependencies except JPA starter
B. Add @SpringBootApplication(exclude = JpaAutoConfiguration.class) on main class
C. Set spring.autoconfigure.enabled=false in application.properties
D. Delete the spring.factories file from the jar

Solution

  1. Step 1: Identify how to exclude specific auto-config

    Spring Boot allows excluding specific auto-config classes using the exclude attribute on @SpringBootApplication.
  2. Step 2: Evaluate other options

    Removing dependencies disables features but is not selective; disabling all auto-config is too broad; deleting spring.factories breaks the framework.
  3. Final Answer:

    Add @SpringBootApplication(exclude = JpaAutoConfiguration.class) on main class -> Option B
  4. Quick Check:

    Exclude specific auto-config via annotation [OK]
Hint: Use exclude attribute on @SpringBootApplication [OK]
Common Mistakes:
  • Disabling all auto-config instead of one
  • Removing dependencies instead of excluding config
  • Modifying internal Spring Boot files
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