Spring Boot @Bean behavior differs in @Component vs @Configuration

🚨 Spring Boot, @Bean inside @Component behaves VERY differently Look at this: @Component public class AppConfig {   @Bean   public OrderService orderService() {     return new OrderService();   } } Looks fine, right? ❌ But this does NOT behave like @Configuration. 🧠 What actually happens at runtime When @Bean is inside @Component: ❌ No CGLIB proxy ❌ No method interception ❌ No singleton guarantee on method calls Calling: orderService(); orderService(); 👉 creates NEW objects every time This mode is called Lite Configuration. ✅ Correct way (Full Configuration Mode) @Configuration public class AppConfig {   @Bean   public OrderService orderService() {     return new OrderService();   } } Here Spring: Creates a CGLIB proxy Intercepts method calls Always returns the same singleton bean 🚨 Why this matters in real projects @Bean public PaymentService paymentService() {   return new PaymentService(orderService()); // 💥 different instance } ➡ Silent bugs ➡ Unexpected behavior ➡ Painful production debugging No exception. No warning. Just wrong behavior. 🎯 Golden Rule @Configuration changes how Java methods behave. @Component does not. #SpringBoot #Java #SpringFramework #Backend #SoftwareEngineering #JVM #SpringTips

  • No alternative text description for this image

@Configuration guarantees singleton behavior via CGLIB proxy — @Component does NOT.

Thanks , I think another example could be when we call a method annotated with @Async or transactional from the same class.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories