@Component vs @Bean — Two Ways to Register Beans
Fast recall
Quick revision
Core answer
@Configuration classes — you write the construction logic explicitly and return the instance Spring should manage. Use @Component for classes you own and want to mark as Spring-managed. Use @Bean for third-party classes you don't control (you can't add @Component to RestTemplate), for conditional or parameterized construction, and when you need multiple beans of the same type with different configs. @Component is convention-based; @Bean is explicit and gives full control over instantiation.Whether the candidate understands that @Component and @Bean serve different scenarios — class ownership vs explicit factory method — and when to reach for each.
Saying they're interchangeable, or trying to put @Component on a third-party class (you can't modify a class you don't own).
Mention that @Bean methods inside @Configuration classes are CGLIB-proxied so calling one method from another returns the same bean, and that this proxying doesn't apply to @Component classes.
Interview-ready guide
Interview answer
Both @Component and @Bean register objects with the Spring container, but they solve different problems.
`@Component` — class-level, scanned
- Annotate a class you own; component scan instantiates it and registers it as a bean
- Specializations:
@Service,@Repository,@Controller - Best for your own services, repositories, controllers — straightforward construction
`@Bean` — method-level, explicit
- Inside a
@Configurationclass, write a method that returns the object Spring should manage - You control everything: constructor choice, arguments, conditional logic
- Best for third-party classes you don't own (
RestTemplate,DataSource), parameterized construction, and when you need multiple beans of the same type
The decision rule
- Do you own the class? If yes and construction is straightforward,
@Component - If no, or if construction needs logic,
@Bean
Subtle but important — CGLIB proxying
@Configurationclasses are subclassed by CGLIB at runtime- Calling one
@Beanmethod from another inside the config class returns the same singleton instance — Spring intercepts the call - This doesn't happen with
@Component-annotated classes — inter-method calls construct fresh instances - Always put
@Beanmethods inside@Configuration, not@Component
Other practical advantages of `@Bean`
- Easy to override in tests (
@TestConfigurationwith same method name) - Easy to register multiple instances of same type with
@Primaryand@Qualifier - Conditional registration via
@ConditionalOnXon the method
@Component vs @Bean — Two Ways to Register Beans
Direct Answer
@Component is a class-level annotation discovered by component scanning — Spring instantiates the class and manages it as a bean. @Bean is a method-level annotation inside @Configuration classes — you write the construction logic explicitly and return the instance Spring should manage. Use @Component for classes you own and want to mark as Spring-managed. Use @Bean for third-party classes you don't control (you can't add @Component to RestTemplate), for conditional or parameterized construction, and when you need multiple beans of the same type with different configs. @Component is convention-based; @Bean is explicit and gives full control over instantiation.
Common Mistakes & Pitfalls
Saying they're interchangeable, or trying to put @Component on a third-party class (you can't modify a class you don't own).
Check your understanding
Answer these without looking back at the article.
- Why are @Configuration classes proxied with CGLIB and what does that affect?
- What happens if you put a @Bean method inside a @Component class instead of @Configuration?
- How do @Primary and @Qualifier resolve ambiguity when multiple beans of the same type exist?
- How would you register a third-party class as a Spring bean?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.