0
0
Spring Bootframework~10 mins

@Component annotation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Component annotation
Start Application
Spring scans classes
Find @Component
Create instance (bean)
Add bean to ApplicationContext
Inject bean where needed
Use bean in app
Spring Boot scans classes for @Component, creates instances, stores them, and injects where needed.
Execution Sample
Spring Boot
import org.springframework.stereotype.Component;

@Component
public class MyService {
  public String greet() { return "Hello"; }
}
Defines a class marked with @Component so Spring creates and manages its instance automatically.
Execution Table
StepActionEvaluationResult
1Start Spring Boot appBegin scanning classesScanning initiated
2Scan MyService classCheck for @Component annotationFound @Component
3Create MyService instanceInstantiate classInstance created
4Register beanAdd instance to ApplicationContextBean stored
5Inject beanFind injection pointsMyService injected where needed
6Use beanCall greet() methodReturns 'Hello'
7EndNo more classes to scanApplication ready
💡 All classes scanned and beans created; application context ready for use.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
MyService instancenullCreated objectStored in ApplicationContextInjected and ready
Key Moments - 2 Insights
Why does Spring create an instance of MyService automatically?
Because the class is marked with @Component, Spring detects it during scanning (see execution_table step 2) and creates an instance (step 3).
How does Spring know where to use the MyService bean?
Spring looks for injection points like @Autowired fields or constructor parameters and injects the bean from ApplicationContext (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
ASpring scans classes for annotations
BSpring injects the bean into other classes
CSpring creates an instance of MyService
DSpring starts the application
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table.
At which step does Spring add the MyService instance to the ApplicationContext?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where the bean is registered or stored in the execution_table.
If the @Component annotation is removed, what changes in the execution flow?
ASpring still creates and injects the bean
BSpring scans but does not create the bean
CSpring throws an error at startup
DSpring creates the bean but does not inject it
💡 Hint
Refer to step 2 where Spring checks for @Component to decide on bean creation.
Concept Snapshot
@Component annotation in Spring Boot:
- Marks a class as a Spring-managed bean
- Spring scans and creates instance automatically
- Bean stored in ApplicationContext
- Injected where needed via @Autowired
- Enables easy dependency management
Full Transcript
When a Spring Boot application starts, it scans all classes for the @Component annotation. When it finds a class marked with @Component, it creates an instance of that class and stores it in the ApplicationContext. Later, Spring injects this instance into other classes where it is needed, for example, using @Autowired. This process helps manage dependencies automatically without manual object creation.