DEV Community

realNameHidden
realNameHidden

Posted on

@PreConstruct and @PostConstruct annotation Spring Boot Example

@PostConstruct: This method is called after the Spring bean (in this case, ExampleBean) has been created and all dependencies have been injected. It’s an ideal place to put initialization logic.

@PreDestroy: This method is called before the Spring bean is destroyed. It’s where you can place cleanup logic, such as closing resources.
Create Spring Boot Application

DemoApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Enter fullscreen mode Exit fullscreen mode

ExampleBean

package com.example.demo;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class ExampleBean {

    public ExampleBean() {
        System.out.println("Constructor: ExampleBean is created");
    }

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

    @PreDestroy
    public void cleanup() {
        System.out.println("PreDestroy: ExampleBean is about to be destroyed");
    }
}

Enter fullscreen mode Exit fullscreen mode

When you run this Spring Boot application, you should see output similar to the following:

Constructor: ExampleBean is created
PostConstruct: ExampleBean is initialized

Enter fullscreen mode Exit fullscreen mode

This output shows that the constructor runs first, followed by the @PostConstruct method.

When you stop the application (e.g., by pressing Ctrl + C if running in the terminal), the @PreDestroy method will be called:

PreDestroy: ExampleBean is about to be destroyed

Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
boma086 profile image
boma086

what is @PreConstruct?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay