π§ 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