0
0
Spring Bootframework~5 mins

@Component annotation in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
Where should you place the @Component annotation?
AOn classes you want Spring to manage
BOn interfaces only
COn methods only
DOn configuration files
Which annotation is a specialized form of @Component for service classes?
A@Repository
B@Service
C@Controller
D@Configuration
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
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>
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.