Skip to main content

Posts

Showing posts with the label Spring Boot

๐Ÿฅ‘๐Ÿฅ’JSON vs HashMap — Why They’re Cousins, Not Twins! ๐Ÿฅ‘๐Ÿฅ’

๐Ÿ— Structure 1️⃣ Setting the Scene — The Confusion Moment ๐Ÿค” "I thought JSON was just a HashMap in a superhero costume!!" Nope! It’s like saying Spring Boot is just Spring MVC with coffee ☕ — there’s way more going on. 2️⃣ ๐Ÿ’ก What is a HashMap? Java-only data structure Stores key-value pairs in memory (heap) Keys can be anything — Integer, String, custom objects (if hashCode() & equals() are implemented properly) Not meant for network transfer ๐Ÿ“ฆ Example: Map<Integer, String> studentMap = new HashMap<>(); studentMap.put(101, "Anand"); studentMap.put(102, "Priya"); This stays inside your JVM. If you try to send it over HTTP as-is… good luck explaining it to a Python service. ๐Ÿ๐Ÿ’ฅ 3️⃣ ๐Ÿ“ What is JSON? Language-independent data format (Java, Python, JS, Go, you name it) Stores only String...

๐Ÿ›’ Spring Bean Scopes – Explained with an E-Commerce Website

☕ Spring Bean Scopes – Explained Like You’re Shopping Online ๐Ÿ›’ In one of my interviews, I was asked a simple question: "What are the scopes in Spring and when will you use them?" I thought I knew it... until the interviewer confused me so much that I started mixing prototype with singleton and session with application ๐Ÿคฏ. So here’s my real-time, e-commerce-style explanation of Spring bean scopes — with code, diagrams, and some fun analogies! 1️⃣ Singleton Scope – ๐Ÿ“ฆ The Universal Product Definition: Spring creates only one instance of the bean for the entire application context. Every @Autowired injection gets the same object . ๐Ÿ›’ E-Commerce Example: Think of Amazon's Shipping Service . Every order uses the same logic, the same configuration — no need to create a new object for each request. @Component @Scope("singleton") // Default one , So you don't want to mention explicitly public class ShippingService { private String shipp...

๐Ÿ’ฅ “Filter vs Interceptor in Spring — Who’s the Real Gatekeeper?”

๐Ÿ’ฅ “Filter vs Interceptor in Spring — Who’s the Real Gatekeeper?” ๐Ÿ” The Hidden Guards of Your REST APIs (with Real Examples, Jokes & Weird Discoveries!) ๐ŸŽฏ 1) What Is Filter / Interceptor / OncePerRequestFilter? ๐Ÿงน Filter A Filter is a component from the Servlet API that sits at the very beginning of a request's journey. It is invoked before the Spring context even gets involved. It's great for tasks like CORS configuration, encoding setup, basic logging, and blocking or modifying raw requests/responses. ๐ŸŽค Think of Filter as the club bouncer who checks everyone at the entrance—before they even see the dance floor! ๐Ÿ•ต️ Interceptor A HandlerInterceptor is a Spring MVC feature that works after the request has passed through the DispatcherServlet but before the controller method is executed. It can inspect the handler (controller) method, add attributes, or block the request if needed. ๐Ÿค“ It’s like the guy inside the club checking if you’re VIP before letting you in ...

๐ŸŽญ Spring’s Secret: Why @Transactional & Friends Betray You Silently

๐Ÿ’ก Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” ๐Ÿ˜ต‍๐Ÿ’ซ “Why is Redis cache not working?” ๐Ÿคฏ Turned out, the issue was one silent villain: ๐Ÿงฑ Self-invocation ๐Ÿคท What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. ๐Ÿง  Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } ๐Ÿ•ต️ Real-Life Analogy — The Gateway Community ๐Ÿ˜️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: ๐Ÿ›‚ Watchman = Spring Proxy ๐Ÿ  Your apartment = Your service class ๐Ÿšช Your room = A method inside that class ๐Ÿƒ Scenario 1: Outsider Visits Your friend from outside...

๐Ÿ’ฅ "Wait... Why Is It NULL?!?" — When @Value("${...}") Ignores Your Static Field ๐Ÿ˜ต‍๐Ÿ’ซ

๐Ÿ”  A surprising Spring Boot moment I didn't expect — and the rabbit hole it took me down... ๐ŸŽฌ It All Started With a Simple Idea... You know how it goes… You define a config value like this in application.properties : payment.currency=INR And then you boldly write this innocent-looking code: @Component public class PaymentUtil { @Value("${payment.currency}") private static String currency; // ✅ Logical! Right? public static void printCurrency() { System.out.println("Currency: " + currency); } } You run the app. And BAM ๐Ÿ’ฅ Currency: null ๐Ÿ˜ต "Eh? I clearly defined the property. Is it not reading the file? Do I need a restart? Do I need to yell at my laptop?" ๐Ÿคฏ The Shocking Discovery Spring doesn’t inject values into static fields. Yes, my friend. Even if you beg it with @Value . And trust me — it’s not a bug. It’s a feature. ๐Ÿ™ƒ ๐Ÿง  What Is This Dependency Injection (DI) Sorcery ...

๐ŸฅŠ @Component vs @Configuration — Not Just Cousins in Spring! ๐Ÿ˜ฒ

 ๐Ÿง  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. @Con...

๐Ÿž Production Bug: 100% CPU Spike from Empty Catch Block — Fixed with Spring Retry! ๐Ÿ˜ฎ๐Ÿ”ฅ

  ๐ŸŒช️ The Chaos Begins It was a normal day in production... until suddenly, one server started heating up ๐Ÿ”ฅ. And I don’t mean metaphorically — its CPU usage shot up to 100% and stayed there, like it had chugged 10 cups of espresso ☕๐Ÿ’ฅ. No OutOfMemoryError. No Exceptions. No logs screaming for help. Just... silence and a fan spinning like a helicopter ๐ŸŒ€. ๐Ÿงฉ The Initial Clues We SSH'd into the server and ran: top -H -p <java_process_id> We noticed one thread hogging the CPU — continuously. Then we used VisualVM to peek inside. Stack trace of that CPU-hogging thread looked like this: at com.example.MyProcessor.process(MyProcessor.java:42) at com.example.MyProcessor.run(MyProcessor.java:30) ... Hmm… That class wasn’t doing anything fancy. Just a retry mechanism inside a loop. ๐Ÿ” The Suspect Code Here’s what the code looked like: while ( true ) { try { doSomeCriticalOperation(); break ; // Exit on success } catch (Exception e) { ...

⏰ Spring Scheduler Calling Multiple Times? Here's What Went Wrong!

 Hey devs ๐Ÿ‘‹, A few years back (around 3–4 years ago), I got a chance to work on a project involving Spring Cron Scheduler — using both XML configuration and Java annotations . The requirement was pretty standard: ✅ Trigger a job every day at a particular time ✅ Fetch data from a table maintained by another team ✅ Process that data further Everything worked well at first — in lower environments and even after initial production deployment . But then... something strange started happening ๐Ÿ˜ฌ ๐Ÿšจ The Weird Behavior After 4-5 days in production, the scheduled method started behaving oddly: ❌ It triggered twice at the same time ❌ Then later, three times , then four ... ๐Ÿ˜ต Restarting the application fixed it — but only temporarily . After a few more days, it would go back to calling multiple times again. It was as if my @Scheduled method had cloned itself and started a party! ๐Ÿฅด ⏳ Take 10 Seconds... Can You Guess the Root Cause? You probably thought of: Duplica...

๐Ÿš€ Demystifying JUnit Testing with Mockito & Spring Extensions

๐Ÿ‘‹ Before diving deep into testing, I always assumed: "JUnit is just something you use with @Test… it just works!"   But once I actually started exploring test frameworks in real-world projects, I realized there's so much more behind the scenes. So here’s a blog to clarify the confusion , make it visually digestible , and hopefully save you some debugging hours . Let’s roll! ๐Ÿง ✨ ๐Ÿ” Step 1: What dependencies should I include for JUnit? You may have seen these terms in your build.gradle : testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' implementation 'com.example:some-library:1.2.3' But wait... what’s the difference? ๐Ÿค” ✅ implementation This means: Include the library for both compile time AND runtime . It will be packaged inside the final JAR/WAR. Use it for things your production code needs. ๐Ÿงช testImplementation This means: Use it only during test execution . It will NOT be added to the production JAR/WAR. Perfect for JU...