Skip to main content

Posts

Showing posts with the label Spring Gotchas

💥 "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 ...