0
0
Spring Bootframework~10 mins

@Component, @Service, @Repository, @Controller in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Component, @Service, @Repository, @Controller
Start Spring Boot App
Scan for Annotations
Find @Component/@Service/@Repository/@Controller
Create Bean Instances
Inject Beans where needed
App runs with managed components
Spring Boot scans for these annotations, creates and manages objects (beans), and injects them where needed to run the app.
Execution Sample
Spring Boot
@Component
public class MyComponent {}

@Service
public class MyService {}

@Repository
public class MyRepository {}

@Controller
public class MyController {}
Defines four classes each marked with a Spring annotation to be managed as beans.
Execution Table
StepActionAnnotation FoundBean CreatedInjection PointResult
1Start app and scan classesNone yetNoNoScanning begins
2Find MyComponent class@ComponentYes: MyComponent beanNoBean ready for injection
3Find MyService class@ServiceYes: MyService beanNoBean ready for injection
4Find MyRepository class@RepositoryYes: MyRepository beanNoBean ready for injection
5Find MyController class@ControllerYes: MyController beanNoBean ready for injection
6Inject beans into dependent classesN/AN/AYesBeans wired where needed
7App runs with all beans managedN/AN/AN/AApp fully functional
💡 All annotated classes are detected, beans created, and injected; app starts successfully.
Variable Tracker
Bean NameStartAfter ScanAfter CreationAfter InjectionFinal
MyComponentNot foundFoundCreatedInjected if neededReady
MyServiceNot foundFoundCreatedInjected if neededReady
MyRepositoryNot foundFoundCreatedInjected if neededReady
MyControllerNot foundFoundCreatedInjected if neededReady
Key Moments - 3 Insights
Why does Spring create objects for these classes automatically?
Because the classes have special annotations like @Component, Spring scans and creates beans for them as shown in execution_table steps 2-5.
What is the difference between @Component and @Service?
@Service is a specialized @Component for service layer logic, but both cause Spring to create beans. See execution_table steps 2 and 3 for both bean creations.
How does Spring know where to put these beans?
Spring injects beans into other classes marked for injection, as shown in execution_table step 6 where injection happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the MyRepository bean created?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Check the 'Bean Created' column for MyRepository in the execution_table rows.
According to variable_tracker, what is the state of MyService after injection?
AInjected if needed
BCreated but not injected
CNot found
DDestroyed
💡 Hint
Look at the 'After Injection' column for MyService in variable_tracker.
If a class is missing @Component or similar annotation, what happens during scanning?
ASpring creates a bean anyway
BSpring ignores the class
CSpring throws an error
DSpring injects it automatically
💡 Hint
Refer to execution_table step 1 where no annotation means no bean creation.
Concept Snapshot
@Component, @Service, @Repository, @Controller are Spring annotations.
They mark classes for Spring to create and manage as beans.
Spring scans these annotations at startup, creates beans, and injects them.
@Service is for service logic, @Repository for data access, @Controller for web controllers.
All are specializations of @Component.
Beans are injected where needed to run the app smoothly.
Full Transcript
In Spring Boot, classes marked with @Component, @Service, @Repository, or @Controller are automatically detected during app startup. Spring scans the code, finds these annotations, and creates bean instances for each. These beans are then injected into other classes that need them, enabling easy reuse and management. @Service is a specialized @Component for business logic, @Repository for data access, and @Controller for handling web requests. This process allows the app to run with all components managed by Spring, simplifying development and wiring.