๐ง Ever looked at these two annotations and thought:
“Aren’t they doing the same thing?”
Welcome to the club. I had the same confusion until... I went deep down the Spring rabbit hole ๐ณ️๐
Let’s clear this once and for all — with real differences, examples, gotchas, and even a few bad jokes along the way ๐
๐ The Basic Definitions
| Annotation | Meaning |
|---|---|
@Component |
A general-purpose stereotype indicating a class is a Spring-managed bean |
@Configuration |
A specialized class used to declare beans via @Bean methods |
๐ก Both register beans with Spring — but what happens behind the scenes is where it gets spicy ๐ถ️
๐ค But Aren’t They Both Just “Beans”?
Short answer: Yes.
Long answer: It’s complicated — like your last relationship ๐.
⚙️ INTERNAL MAGIC: The Real Difference
1. @Configuration Uses CGLIB Proxy for Singleton ๐ซ
When you mark a class with @Configuration, Spring uses CGLIB subclassing (AOP-style proxying) to make sure all @Bean methods return the same singleton — even if called multiple times.
๐ฃ Example:
@Configuration
public class MyConfig {
@Bean
public Engine engine() {
return new Engine(); // always returns same Engine
}
@Bean
public Car car() {
return new Car(engine()); // uses proxied engine()
}
}
๐ Result:
Spring intercepts engine() call inside car() and reuses the same singleton object.
2. @Component Doesn’t Use CGLIB Proxy ๐
If you do this:
@Component
public class MyConfig {
@Bean
public Engine engine() {
return new Engine(); // ๐ต This gets called multiple times!
}
@Bean
public Car car() {
return new Car(engine()); // Creates a new Engine every time
}
}
๐ฅ Boom! Now car() and engine() use different objects — no proxying here.
๐ง This is why Spring recommends using @Configuration for bean factories, not @Component.
๐ก️ Singleton Is Not Just a Pattern — It’s a Proxy Game
Without CGLIB proxying, Spring cannot guarantee singleton behavior across bean methods. That’s why:
@Configuration= Proxy-magic ๐ช + Full bean lifecycle control@Component= Regular class ๐งฑ with Spring bean creation only
๐ต️♂️ Real-World Gotcha You Didn’t Know
❗ Changing @Configuration to @Component breaks singleton!
You might have seen this subtle bug in interviews or production:
You might have seen this subtle bug in interviews or even production:
@Configuration
public class MyConfig {
...
}
➡️ Someone changes it to:
@Component
public class MyConfig {
...
}
๐ฏ Guess what? It still works… but beans aren’t singleton anymore ๐ฑ
✅ Why We Use @Component — And How Spring Knows What to Wire ๐
Let’s say you wrote a simple utility class like this:
Now… how does Spring know this class even exists? And how do we use it?
๐ Step 1: Mark with @Component
This tells Spring:
“Hey, please manage this class. Make an object, keep it ready, I’ll be needing it.” ๐
Spring adds it to the ApplicationContext during startup (if it's in a scanned package).
๐ Step 2: Inject It Using @Autowired or Constructor Injection
Now Spring will auto-wire EmailUtil wherever it's needed!
๐ So the Flow Looks Like This:
-
You annotate
EmailUtilwith@Component✅ -
Spring finds it via
@ComponentScanduring startup ๐ -
You use
@Autowired(or constructor injection) to inject it into another class ๐ -
Spring sees the request and wires the singleton instance of
EmailUtil๐ช
๐คฏ What If You Don’t Annotate It with @Component?
Then Spring won’t even know it exists ❌
→ You'll get a NoSuchBeanDefinitionException when you try to inject it.
๐ญ Use Cases — When to Use What?
| Use This For... | Annotation |
|---|---|
| Declaring beans with @Bean | @Configuration ✅ |
| General Spring component scan | @Component ✅ |
๐ Funny Analogy
Imagine @Component is like a coffee shop ๐ช.
You order coffee ☕, and they make a new one each time.
But @Configuration is like your mom’s kitchen ๐ —
She remembers what you ordered last week and says:
"Son, manage with one cup... memory is limited here too, like our heap space!" ๐ง ๐♀️
๐ง Summary — The Final Showdown
| Feature | @Component | @Configuration |
|---|---|---|
| Spring Bean Registration | ✅ | ✅ |
| Proxy Bean Method Calls | ❌ | ✅ (CGLIB) |
| Ensures Singleton | ❌ | ✅ |
| Use with @Bean methods | ๐ซ Avoid | ✅ Recommended |
๐จ Bonus: Colorful Icons for Memory ๐ง
-
๐ง
@Component= Generic Tool -
๐️
@Configuration= Master Builder with Memory -
๐งช
@Bean= Custom-Made Ingredient -
๐ CGLIB = Behind-the-scenes Repeater
๐คน Wrapping Up
So next time someone says “@Component and @Configuration are same,”
just drop this truth bomb ๐ฃ:
Only one of them respects Singleton. The other one? It’s freelancing ๐
๐ฌ Got more such hidden Spring secrets or confusion?
Drop a comment — I love exploring the “whys” behind the “hows”!
Comments
Post a Comment