Spent 15 minutes wondering why my @Value annotation wasn't injecting the property. The code: @Component public class AppConfig { @Value("${app . name}") private String appName; public AppConfig() { System.out.println(appName); } } It printed null every time. The problem: @Value injection happens after the constructor runs. In the constructor, Spring hasn't injected anything yet. The fix: Use @PostConstruct instead: @PostConstruct public void init() { System.out.println(appName); } Or use constructor injection: public AppConfig(@Value("${app . name}") String appName) { this.appName = appName; } Constructor runs first. Injection happens after. Simple timing issue. Easy to miss. #SpringBoot #Java #BackendDevelopment #Programming
Spring @Value Annotation Injection Timing Issue
More Relevant Posts
-
💡 Day 43 Solved "House Robber" Problem #198 using Java! Today I worked on a classic Dynamic Programming problem on LeetCode – House Robber 🏠💰 🔍 Problem: Find the maximum amount of money you can rob without robbing two adjacent houses. 🧠 Approach: At every step, we decide: ✔️ Rob current house → add value + profit from i-2 ✔️ Skip current house → take profit from i-1 We choose the maximum of these two options. 🚀 Optimization: Instead of using a DP array, I used two variables to reduce space complexity to O(1). ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem is a great example of how dynamic programming simplifies complex decisions! #LeetCode #Java #DynamicProgramming #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
So last week, while I was internally implementing a basic version of the C++ STL vector, I was studying how to make it more efficient and be careful about the memory, in java garbage collection took care of the unreferenced heap memory but in C++, I was thinking how to handle it, and in various explanations, I saw a trick ( a pattern to be precise ), 💡 The idea is simple but elegant: • Tie the lifetime of a resource (memory, file, mutex, etc.) to the lifetime of an object • Acquire the resource in the constructor • Release it automatically in the destructor Its a powerful design principle: RAII (Resource Acquisition Is Initialisation). The destructor is automatically called when an object goes out of scope (during normal execution). This means cleanup is handled deterministically, without relying on the programmer to remember it. For example, if we release allocated memory inside the destructor, we don’t have to worry about leaks. This becomes especially important during exceptions when stack unwinding occurs, destructors are still called, ensuring resources are properly freed. Why RAII is powerful: • Prevents memory leaks • Exception safe by design • Works for all kinds of resources (not just memory) • Forms the backbone of modern C++ (smart pointers, STL containers, etc.) This concept helped me understand and write safe and reliable C++ code. Happy Coding :) #cpp #cplusplus #programming #softwareengineering #systemsprogramming #lowlevelprogramming #codingconcepts
To view or add a comment, sign in
-
-
🚨 Spring Boot gotcha that cost me hours… I thought my "@Transactional" was working perfectly… Until it silently stopped working 😶 The culprit? 👉 Self Invocation (internal method calls) --- 💥 What works: When a method is called from outside the bean ➡️ Spring Proxy intercepts the call ➡️ Transaction starts ✅ --- ❌ What DOESN’T work: When a method calls another method inside the same class this.placeOrderInternal(); // ❌ bypasses proxy ➡️ No proxy ➡️ No transaction ➡️ No rollback 😬 --- ⚠️ Same problem happens with: - "@Async" - Custom annotations (AOP) - Multi-tenant filters (ThreadLocal based) 👉 Especially when using "@Async" Your logic runs in a different thread ➡️ Filter context is LOST ➡️ Tenant info = NULL 💣 --- 🧠 Why this happens? Spring uses proxies (AOP) No proxy = No magic --- 🛠️ Fixes: ✔️ Call method from another bean ✔️ Inject self proxy ✔️ Use "TransactionTemplate" ✔️ Pass context manually for "@Async" (ThreadLocal won’t work automatically) --- 💡 Real lesson: If Spring can't intercept your call… 👉 Your annotation is just decoration. --- Have you ever debugged something like this for hours? 😅 Drop your experience 👇 #SpringBoot #Java #Backend #Microservices #Programming #100DaysOfCode
To view or add a comment, sign in
-
-
Day 23 ✅ — still showing up every day. #100DaysOfCode | LeetCode 151 – Reverse Words in a String Looks easy on the surface. Reverse the words in a string. Simple, right? Not quite. The tricky part? The input can have leading spaces, trailing spaces, AND multiple spaces between words — but the output should be clean with single spaces only. My Java solution: — Trim the input — Split by space — Loop backwards, skip empty strings from the split — Build the result using StringBuilder Final runtime: 5ms | Beats 86.54% 🎯 Honest takeaway: This problem reminded me that edge cases are where real programming lives. It's not just about getting the logic right — it's about thinking about what can go wrong. 23 days in. 77 to go. Let's keep building. 🙌 #LeetCode #DSA #Java #100DaysOfCode #DailyChallenge #CodeNewbie #TechCommunity
To view or add a comment, sign in
-
-
Sharing a one-page cheat sheet that covers essential Spring Boot concepts for developers. This includes: - Key features & architecture - REST controller example - Dependency Injection (best practices) - Exception handling - Actuator & logging - Profiles & configuration This resource is perfect for quick revision and backend interview preparation. #SpringBoot #Java #BackendDevelopment #JavaDeveloper #Programming #InterviewPreparation
To view or add a comment, sign in
-
-
Many beginners use Spring annotations like @Autowired and @Component daily, but don’t fully understand what happens behind the scenes. The real magic happens inside the Spring IoC Container. Here’s the step-by-step flow: Spring reads configuration Bean definitions are created IoC container initializes Beans are instantiated Dependencies are injected Lifecycle methods are called Beans become ready to use Destroy methods run when the application stops Without IoC: You manually create objects and manage dependencies. With Spring IoC: Spring creates, manages, and injects everything for you. Example: Engine engine = new Engine(); Car car = new Car(engine); vs Car car = context.getBean(Car.class); That’s why Spring applications stay cleaner, more scalable, and easier to maintain. Key concepts: BeanFactory ApplicationContext Dependency Injection Bean Lifecycle Autowiring Bean Scope If you are learning Spring Boot, understanding IoC is one of the most important fundamentals. #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #Programming #Developers #Coding #JavaDeveloper #DependencyInjection #SpringFramework
To view or add a comment, sign in
-
-
🧠 After learning Dependency Injection, I had one big question 👀 Who actually creates these objects in Spring Boot, and when? Today I explored the Spring Bean Lifecycle 🚀 Simple flow 👇 1️⃣ Spring container starts 2️⃣ Bean object is created 3️⃣ Dependencies are injected 4️⃣ Bean becomes ready to use 5️⃣ Bean is destroyed when the app shuts down What made this even more interesting 👇 ✅ @PostConstruct → runs after bean creation ✅ @PreDestroy → runs before bean cleanup 💡 My takeaway: Spring doesn’t just inject dependencies — it also manages the entire lifecycle of the object. The more I learn this framework, the more beautifully engineered it feels ⚡ #Java #SpringBoot #BeanLifecycle #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
Find the minimum distance between a given start index and any occurrence of a target in the array. Leetcode problem link: https://lnkd.in/gZFkirHw 🔍 Key Takeaways: Used a two-pointer approach to scan from both ends. Kept updating the minimum absolute distance from start. Included the condition left != right to avoid checking the same element twice. ✅ Why this works: Instead of scanning only from one side, this approach checks both ends in each iteration, making the logic structured and efficient. 📘 What I like about this solution: Simple and readable Avoids redundant checks Great example of combining two pointers with distance calculation #Java #LeetCode #DSA #Programming #SoftwareEngineering #Developers #loveToCode
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Same thing applies to @Autowired fields. If you need them in the constructor, use constructor injection instead. Field injection happens after object creation.