DEV Community

Cover image for 🌿 Spring Bean Lifecycle & Dependency Injection — What I Just Learned (From a Student's POV)
Arkadipta kundu
Arkadipta kundu

Posted on

🌿 Spring Bean Lifecycle & Dependency Injection — What I Just Learned (From a Student's POV)

Yo! So I’ve been on this wild ride learning Java + Spring lately, and the deeper I get into Spring, the more things start to click. Last week, I focused on two core ideas: Dependency Injection (DI) and the Spring Bean Lifecycle — and trust me, these two are literally the building blocks of anything Spring does.

Here’s what I picked up — explained the way I understood it. So if you’re also just starting out with Spring, maybe this helps you connect the dots too. 🤝


📚 Resources I Used

These helped me make sense of it all:


⚙️ Dependency Injection (DI)

So this one is like the "engine" behind how Spring manages objects (aka beans). Instead of you manually wiring dependencies using new, Spring handles that automatically.

Here are the types I learned:

💉 Constructor Injection

This is when dependencies are provided through the constructor.

@Component
public class Car {
    private final Engine engine;

    @Autowired
    public Car(Engine engine) {
        this.engine = engine;
    }
}
Enter fullscreen mode Exit fullscreen mode

✅ Great for making fields final (i.e., you can’t forget to inject something).

🔧 Setter Injection

Here, Spring uses setters to inject dependencies.

@Component
public class Car {
    private Engine engine;

    @Autowired
    public void setEngine(Engine engine) {
        this.engine = engine;
    }
}
Enter fullscreen mode Exit fullscreen mode

✅ Useful when the dependency is optional or might change after instantiation.

🤔 Which one should you use?
From what I gathered: Constructor Injection is usually preferred, because it makes dependencies mandatory and easier to test.


🌱 Spring Bean Lifecycle (Simple Breakdown)

Alright, so Spring manages your classes as beans, and every bean goes through a lifecycle managed by the container. Here’s the flow I understood:

  1. Instantiation — Object is created
  2. Populate Properties — Dependencies are injected
  3. Initialization — Bean is ready to use
  4. Destruction — Cleanup time (when context is closed)

Now, how do you hook into this lifecycle?

🛠️ @PostConstruct and @PreDestroy

@Component
public class MyBean {

    @PostConstruct
    public void init() {
        System.out.println("Bean is initialized!");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Bean is about to be destroyed!");
    }
}
Enter fullscreen mode Exit fullscreen mode

These annotations come from javax.annotation and they’re super handy for running some setup or cleanup code automatically.

🧾 init-method and destroy-method in XML or Java Config

If you're not using annotations, you can also declare init/destroy methods in config:

@Bean(initMethod = "init", destroyMethod = "cleanup")
public MyBean myBean() {
    return new MyBean();
}
Enter fullscreen mode Exit fullscreen mode

Spring will call init() after bean creation and cleanup() before destroying the bean.


🧠 Why Does This Matter?

At first, it felt like “meh, just lifecycle stuff,” but when you start writing real-world Spring applications, this helps in:

  • Managing DB connections
  • Releasing resources (files, sockets, etc.)
  • Initializing third-party libraries
  • Prepping beans before use

Spring gives you full control without the chaos — and once you know this flow, building apps gets way smoother.


🚧 What I’ll Learn Next

I'm still in the early phase, so next up I’ll be:

  • Deep diving into annotation-based configuration
  • Exploring @ComponentScan, @Configuration, @Bean
  • And then eventually getting my hands dirty with Spring Boot! 🧪

🧾 All My Practice Code

👨‍💻 GitHub: Spring Framework Related Codes

Feel free to explore and suggest improvements — I'm learning as I go!


🥜 In a nutshell (TL;DR)

  • Spring handles dependencies using Dependency Injection (DI) — either via constructors or setters.
  • Beans have a lifecycle: they’re created, initialized, and eventually destroyed.
  • You can tap into lifecycle events using:
    • @PostConstruct for setup
    • @PreDestroy for cleanup
  • Alternative config styles include init-method and destroy-method.
  • Understanding the bean lifecycle helps in managing resources and building more maintainable apps.

That’s it from my side for now. Spring is huge, but breaking it down into bite-sized parts like this is helping me stay on track. If you're also learning Spring or Java, let's connect and grow together 🚀

Top comments (0)