DEV Community

realNameHidden
realNameHidden

Posted on

2 1 1 1 1

@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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
boma086 profile image
boma086

what is @PreConstruct?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay