0
0
Spring Bootframework~30 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a hint?

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