0
0
Spring Bootframework~10 mins

@Configuration and @Bean in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Configuration and @Bean
Start Application
Scan for @Configuration
Create Configuration Class Instance
Call @Bean Methods
Register Returned Objects as Beans
Beans Ready for Injection
Application Runs with Beans
Spring Boot starts by scanning for classes marked @Configuration, then calls methods marked @Bean to create and register objects for use in the app.
Execution Sample
Spring Boot
@Configuration
public class AppConfig {
  @Bean
  public Service service() {
    return new Service();
  }
}
Defines a configuration class that creates a Service bean for the application context.
Execution Table
StepActionEvaluationResult
1Start Spring Boot applicationScan classesFind AppConfig with @Configuration
2Create instance of AppConfigInstantiate classAppConfig object created
3Call service() methodExecute @Bean methodNew Service object created
4Register Service objectAdd to Spring contextService bean available
5Inject Service bean where neededDependency injectionService ready to use
6Application runsBeans used in appApp works with Service bean
💡 All @Bean methods called and beans registered; application context ready.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
AppConfig instancenullcreatedcreatedcreatedcreated
Service beannullnullcreatedregisteredavailable
Key Moments - 3 Insights
Why does Spring call the @Bean method only once?
Spring calls the @Bean method once to create a single shared bean instance, as shown in step 3 and 4 of the execution_table.
What happens if @Configuration is missing?
Without @Configuration, Spring does not treat the class as a source of beans, so @Bean methods are not called automatically (see step 1 in execution_table).
How does Spring know to inject the Service bean?
Spring registers the Service object as a bean (step 4) and injects it where needed by matching type or name during dependency injection (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the Service bean created?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Action' and 'Result' columns in step 3 where the Service object is created.
According to variable_tracker, what is the state of the Service bean after Step 4?
Anull
Bregistered
Ccreated
Davailable
💡 Hint
Look at the 'Service bean' row and the column 'After Step 4' in variable_tracker.
If @Configuration annotation is removed, which step in execution_table would fail?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Step 1 shows scanning for @Configuration; without it, Spring won't find the config class.
Concept Snapshot
@Configuration marks a class as a source of bean definitions.
@Bean marks methods that create beans.
Spring calls @Bean methods once to create and register beans.
Beans are managed and injected by Spring automatically.
Without @Configuration, @Bean methods are not processed.
Full Transcript
When a Spring Boot application starts, it scans for classes annotated with @Configuration. These classes are special because they tell Spring where to find bean definitions. Spring creates an instance of the configuration class and then calls each method marked with @Bean exactly once. Each @Bean method returns an object that Spring registers as a bean in its application context. These beans are then ready to be injected wherever needed in the application. If the @Configuration annotation is missing, Spring will not recognize the class as a configuration source, so the @Bean methods will not be called, and no beans will be registered from that class.