For a long time, I used @Autowired and @Bean without really thinking about what happens in between. The bean exists. It works. Move on. But going deep on Spring Boot means stopping at the things that just work and asking how exactly it happens. The bean lifecycle is one of those things. Spring doesn't just create your object and hand it to you. It goes through a sequence like parsing the definition, instantiation, dependency injection, Aware interfaces, post-processors, initialisation, and eventually destruction. Each phase has a purpose. And each one is a place where things can go quietly wrong if you don't know it exists. The one that surprised me most is you cannot rely on @Autowired fields inside a constructor, because dependency injection hasn't happened yet at that point. The constructor runs first. The fields are populated later. That kind of detail doesn't show up in tutorials. It shows up when something behaves unexpectedly and you have no idea where to look. This article by Hüsna Poyraz explains the full lifecycle clearly, worth reading if you're learning Spring Boot seriously. Link in the comments. #SpringBoot #Java #BackendDevelopment
Spring Boot Bean Lifecycle Explained
More Relevant Posts
-
🚨 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
-
-
3 Spring annotations that look similar… but do completely different things @ComponentScan @Configuration @EnableAutoConfiguration If you’re learning Spring Boot, this trio can be seriously confusing. Let’s break it down in the simplest way possible 👇 Think of Spring like setting up a smart system in your house Each of these annotations plays a different role: @𝗖𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁𝗦𝗰𝗮𝗻 → “Find everything” It tells Spring: “Go and scan this package and find all the classes marked with @Component, @Service, etc.” Without this: Spring won’t even know your classes exist @𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻 → “Define how things are created” It tells Spring: “This class contains methods that create objects (using @Bean)” Think of it as: A place where you manually configure your app @𝗘𝗻𝗮𝗯𝗹𝗲𝗔𝘂𝘁𝗼𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻 → “Spring handles it for you” This is the magic part. Spring automatically: Configures database connections Sets up web servers Wires dependencies Based on: What’s in your dependencies (pom.xml) Fun fact: @𝗦𝗽𝗿𝗶𝗻𝗴𝗕𝗼𝗼𝘁𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻 = combination of all three That’s why Spring Boot feels so simple. Once you understand this, you stop seeing Spring as “magic” …and start seeing it as a system you control. #CoreJava #BackendDevelopment #SpringBoot #Spring #Annotations #Framework #JavaDevloper #coding #ProgrammingBasics #aswintech
To view or add a comment, sign in
-
📘 Spring Framework Today I explored how Spring handles dynamic values, external configuration, and stereotype annotations. 🔹 Spring Expression Language (SpEL) 🔹 Using @Value for injecting properties 🔹 Externalized configuration with properties files 🔹 Stereotype annotations (@Component, @Service, @Repository, @Controller) 🔹 Understanding how Spring organizes application layers What I’m realizing while learning Spring is that it’s not just about creating beans — it’s about managing application flow in a structured and maintainable way 💡 Documenting my journey one concept at a time 🚀 From next Post... Journey to Spring Boot will begin... #SpringFramework #JavaDeveloper #SpringBoot #BackendDevelopment #LearnInPublic #CodingJourney
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
-
-
If your Spring Boot class uses field injection, you’re making your life harder. Not immediately...but the problems show up when your system grows. In older Spring Boot codebases, field injection still shows up everywhere. It works with no complaints. Until you need to change something or test it properly. Constructor injection changes how the class behaves. - Dependencies are fixed at creation. - Missing ones fail fast. - The class becomes predictable. Where this really shows up is testing! With field injection, you rely on Spring to build the class. With constructor injection, you can just instantiate it. No context, no wiring just the class and your test. Spring already handles this for you. If there’s a single constructor, it’s used automatically (since Spring 4.3) with no extra annotations needed. BUT...@Autowired still has its place if you have: - Multiple constructors - Specific edge cases You just won’t reach for it often. #Java #SpringBoot #SpringFramework #BackendDevelopment
To view or add a comment, sign in
-
-
🚀 Spring Boot Exception Handling – Centralized Approach (Global Exception Handler) Today I learned an important concept in Spring Boot: 👉 Centralized Exception Handling using @RestControllerAdvice Instead of writing try-catch everywhere in controllers, we can create a single global class to handle all exceptions in one place. --- 💡 Why do we need it? Without global exception handling: ❌ Repeated try-catch in every API ❌ Messy controller code ❌ Hard to maintain error responses With global exception handling: ✅ Clean and readable controllers ✅ Consistent error response format ✅ Easy maintenance ✅ Better debugging --- 🧠 How it works? We create a class like this: ✔️ "@RestControllerAdvice" → Makes it a global exception handler ✔️ "@ExceptionHandler" → Handles specific exceptions ✔️ Custom response → We can structure error messages properly --- 📌 Example flow: If an error occurs in any controller → 👉 It will automatically go to Global Exception Handler 👉 And return a proper response like: { "timestamp": "2026-04-13", "message": "Something went wrong", "status": 500 } --- 🔥 Key Takeaway: Centralized exception handling makes Spring Boot applications: ✔ Cleaner ✔ Professional ✔ Scalable --- Today’s learning: small concept but very powerful for real-world backend development 💻 #SpringBoot #Java #BackendDevelopment #ExceptionHandling #LearningJourney #Programming
To view or add a comment, sign in
-
-
🚀 Day 20/100: Spring Boot From Zero to Production Topic: Custom Auto-Configuration We covered Auto-Configuration. We covered disabling it. But does it stop there? Nope. 👀 Spring Boot lets you build your own auto-configuration too. 🔧 But, How It Works? You create a @Configuration class and slap conditionals on it. Spring Boot only loads it if your conditions are met. Two most common ones: @ConditionalOnClass → Load only if a class exists on the classpath @ConditionalOnProperty → Load only if a property is set in application.properties 💡 See the code attached below. ⬇️ No DataSource on classpath? → Skipped entirely db.enabled=false? → Skipped entirely Bean already defined by user? → Your default is skipped ✅ 📌 Don't forget to register it In META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -> com.yourpackage.MyDataAutoConfiguration Without this, Spring Boot won't pick it up. Checkout the previous posts on Auto-Config & disabling it to better understand the sequence. See you in the next one! #Java #SpringBoot #SoftwareDevelopment #100DaysOfCode #Backend #AutoConfiguration
To view or add a comment, sign in
-
-
I used to think Spring Boot just runs and handles everything Swipe to see what is actually happening inside If your answer to how Spring Boot works is it just runs and handles requests you are probably missing what actually matters Most people can build with Spring Boot very few understand what happens under the hood While going deeper, a few things started making more sense • How the application starts and builds the context • What SpringApplication triggers behind the scenes • How beans are created and managed step by step • Why proxies exist and where they actually matter • How self invocation can silently break transactions The more I learn, the more I realise it is not just about writing code it is about understanding how the system behaves Still learning, but this changed how I approach backend development What part of Spring Boot internals do you find confusing #Java #SpringBoot #BackendDevelopment #SoftwareEngineering #SystemDesign
To view or add a comment, sign in
-
Yesterday: “It’s working perfectly 😌” Today: Application failed to start. Same code. Same logic. Different story. Welcome to Spring Boot reality 👇 → One missing bean = full system collapse → One wrong config = hours gone → One hidden null = chaos unleashed → One DB hiccup = everything breaks And suddenly… you’re not coding anymore, you’re investigating. Logs become clues. Stack traces become stories. Debugging isn’t pain — it’s where real backend thinking begins. Because great developers don’t fear crashes… they understand them. ⚡ #SpringBoot #Java #BackendDevelopment #Debugging #BuildInPublic
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
https://medium.com/but-it-works-on-my-machine/spring-bean-lifecycle-how-beans-are-managed-behind-the-scenes-ce1d2f58318d