0
0
Spring Bootframework~10 mins

Bean concept in Spring in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bean concept in Spring
Start Application
Spring Container Initializes
Scan for @Component/@Bean
Create Bean Instances
Inject Dependencies
Beans Ready to Use
Application Runs Using Beans
Shutdown -> Beans Destroyed
Spring starts, scans for beans, creates and injects them, then runs the app using these beans.
Execution Sample
Spring Boot
@Component
public class MyService {
  @Autowired
  private MyRepository repo;
}
Defines a bean MyService with an injected dependency MyRepository.
Execution Table
StepActionBean NameStateDetails
1Start Spring Application-InitializingSpring container starts up
2Scan Classes-ScanningFinds @Component MyService and @Repository MyRepository
3Create BeanmyRepositoryCreatedInstance of MyRepository created
4Create BeanmyServiceCreatedInstance of MyService created
5Inject DependencymyServiceDependency InjectedmyRepository injected into myService
6Beans Ready-ReadyBeans are ready for use in application
7Application Running-RunningApplication uses beans to handle requests
8Shutdown-DestroyedBeans destroyed and container closed
💡 Spring application stops, beans are destroyed and container shuts down
Variable Tracker
BeanStartAfter CreationAfter InjectionFinal State
myRepositoryNot CreatedCreatedN/AReady
myServiceNot CreatedCreatedInjected myRepositoryReady
Key Moments - 3 Insights
Why does Spring create beans automatically?
Spring scans classes with @Component or @Bean annotations and creates instances to manage them, as shown in steps 2-4 in the execution table.
How does dependency injection work in Spring beans?
Spring injects required dependencies into beans after creating them, like myRepository injected into myService at step 5.
What happens to beans when the application stops?
Beans are destroyed and resources cleaned up during shutdown, as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the myService bean created?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Check the 'Bean Name' and 'Action' columns in the execution table rows 3 and 4.
According to the variable tracker, what is the state of myRepository after injection?
ANot Created
BCreated
CInjected into myService
DDestroyed
💡 Hint
Look at the 'After Creation' and 'After Injection' columns for myRepository in the variable tracker.
If the @Autowired annotation was missing on myService, what would change in the execution table?
AStep 3 would not create myRepository
BStep 4 would not create myService
CStep 5 would not inject myRepository into myService
DBeans would not be destroyed at shutdown
💡 Hint
Focus on the 'Inject Dependency' action in step 5 of the execution table.
Concept Snapshot
Spring Bean Concept:
- Beans are objects managed by Spring container.
- Use @Component or @Bean to define beans.
- Spring auto-creates and injects dependencies (@Autowired).
- Beans lifecycle: creation -> injection -> use -> destruction.
- Beans enable loose coupling and easier testing.
Full Transcript
When a Spring application starts, the Spring container initializes and scans the code for classes marked as beans using annotations like @Component or @Bean. It then creates instances of these beans. After creation, Spring injects any dependencies marked with @Autowired into these beans. Once all beans are ready, the application runs using these managed objects. When the application stops, Spring destroys the beans and cleans up resources. This process helps manage object lifecycles and dependencies automatically.