What is the purpose of the @Component annotation in Spring Boot?
The <code>@Component</code> annotation tells Spring to create and manage an instance of the class as a bean. It makes the class available for dependency injection.
Click to reveal answer
beginner
How does Spring Boot find classes annotated with @Component?
Spring Boot scans the packages specified in the application for classes with @Component and automatically registers them as beans in the application context.
Click to reveal answer
beginner
Can @Component be used on interfaces or only on classes?
<code>@Component</code> should be used on classes, not interfaces, because Spring creates an instance of the class to manage as a bean.
Click to reveal answer
intermediate
What is the difference between @Component and @Service annotations?
@Service is a specialized form of @Component used to mark service layer classes. Functionally they are similar, but @Service adds semantic meaning.
Click to reveal answer
intermediate
How can you give a custom name to a bean created with @Component?
You can specify a name by passing a string value to @Component("customName"). This name can be used to refer to the bean in the Spring context.
Click to reveal answer
What does the @Component annotation do in Spring Boot?
ADefines a database entity
BSpecifies a REST controller
CMarks a class as a Spring-managed bean
DConfigures application properties
✗ Incorrect
@Component marks a class so Spring creates and manages it as a bean.
Where should you place the @Component annotation?
AOn classes you want Spring to manage
BOn interfaces only
COn methods only
DOn configuration files
✗ Incorrect
@Component is used on classes to tell Spring to create beans from them.
Which annotation is a specialized form of @Component for service classes?
A@Repository
B@Service
C@Controller
D@Configuration
✗ Incorrect
@Service is a specialized @Component for service layer classes.
How does Spring Boot find classes annotated with @Component?
ABy scanning specified packages automatically
BBy manual registration only
CBy reading XML files
DBy runtime user input
✗ Incorrect
Spring Boot scans packages to find @Component classes and registers them automatically.
How can you assign a custom name to a bean created with @Component?
AUsing <code>@Autowired("name")</code>
BUsing <code>@Bean(name="name")</code>
CUsing <code>@Service(name="name")</code>
DUsing <code>@Component("name")</code>
✗ Incorrect
You can pass a string to @Component to name the bean.
Explain what the @Component annotation does in Spring Boot and why it is useful.
Think about how Spring knows which classes to create and manage.
You got /4 concepts.
Describe how you can customize the name of a bean created with @Component and why you might want to do that.
Consider how Spring identifies beans in the application context.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the @Component annotation in Spring Boot?
easy
A. To mark a class so Spring automatically creates and manages its instance
B. To define a database entity
C. To configure application properties
D. To handle HTTP requests
Solution
Step 1: Understand the role of @Component
The @Component annotation tells Spring to create an instance of the class and manage it automatically.
Step 2: Compare with other options
The other options relate to other Spring features like entities, properties, or controllers, not @Component.
Final Answer:
To mark a class so Spring automatically creates and manages its instance -> Option A
Quick Check:
@Component = automatic management [OK]
Hint: Remember: @Component means Spring manages the class [OK]
Common Mistakes:
Confusing @Component with @Entity
Thinking it handles HTTP requests
Assuming it configures properties
2. Which of the following is the correct way to declare a Spring component class?
easy
A. @Component public class MyService {}
B. @Service public void MyService() {}
C. public class MyService {}
D. @Component void MyService() {}
Solution
Step 1: Check annotation placement and class declaration
The @Component annotation must be placed above a class declaration, like @Component public class MyService {}.
Step 2: Identify incorrect syntax
A plain public class misses the annotation, annotating a method with @Service or @Component is invalid since components must be classes.
Final Answer:
@Component public class MyService {} -> Option A
Quick Check:
Annotation + class declaration = correct syntax [OK]
Hint: Always put @Component above a class, not a method [OK]
Common Mistakes:
Annotating methods instead of classes
Missing the annotation entirely
Using wrong annotations for components
3. Given the code below, what will be the output when app.run() is called?
@Component
public class App {
@Autowired
private ServiceA serviceA;
public void run() {
System.out.println(serviceA.getName());
}
}
@Component
public class ServiceA {
public String getName() {
return "ServiceA";
}
}
medium
A. Runtime NullPointerException
B. null
C. ServiceA
D. Compilation error
Solution
Step 1: Understand component scanning and injection
Both App and ServiceA are annotated with @Component, so Spring creates and manages their instances. The @Autowired injects ServiceA into App.
Step 2: Trace the method call
Calling app.run() prints the result of serviceA.getName(), which returns "ServiceA".
Final Answer:
ServiceA -> Option C
Quick Check:
Autowired injects component = output string [OK]
Hint: If both classes are @Component, autowired works and prints value [OK]
Common Mistakes:
Assuming serviceA is null without injection
Confusing compilation vs runtime errors
Forgetting to annotate both classes
4. What is the problem with the following code snippet?
@Component
public class MyComponent {
@Autowired
private ServiceB serviceB;
public void doWork() {
serviceB.perform();
}
}
Assuming ServiceB is not annotated with @Component or any stereotype, what will happen?
medium
A. Compilation error due to missing import
B. The code will compile and run fine
C. ServiceB will be automatically created by Spring
D. Spring will throw a runtime error due to missing bean
Solution
Step 1: Check bean creation requirements
Spring only manages classes annotated with @Component or similar. Since ServiceB lacks this, Spring won't create its bean.
Step 2: Understand autowiring consequences
When Spring tries to inject serviceB, it finds no bean and throws a runtime error (NoSuchBeanDefinitionException).
Final Answer:
Spring will throw a runtime error due to missing bean -> Option D
Quick Check:
Missing @Component = runtime injection error [OK]
Hint: All injected classes must be annotated for Spring to manage [OK]
Common Mistakes:
Assuming Spring creates beans without annotation
Confusing compile errors with runtime errors
Forgetting to annotate dependencies
5. You want to create two different implementations of an interface PaymentService and have Spring inject the correct one based on a custom qualifier. How should you use @Component to achieve this?
hard
A. Annotate only one implementation with @Component and inject by interface type
B. Annotate both implementations with @Component and use @Qualifier to specify which to inject
C. Use @Component only on the interface, not implementations
D. Create instances manually without @Component
Solution
Step 1: Annotate both implementations
Both classes implementing PaymentService must be annotated with @Component so Spring manages them.
Step 2: Use @Qualifier to distinguish
When injecting, use @Qualifier with the component name to tell Spring which implementation to use.
Final Answer:
Annotate both implementations with @Component and use @Qualifier to specify which to inject -> Option B