Logo

dev-resources.site

for different kinds of informations.

@PreConstruct and @PostConstruct annotation Spring Boot Example

Published at
1/5/2025
Categories
java
interview
spring
boot
Author
realnamehidden1_61
Categories
4 categories in total
java
open
interview
open
spring
open
boot
open
Author
18 person written this
realnamehidden1_61
open
@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
spring Article's
30 articles in total
Favicon
Launched a Web version of my Project using Java Spring Framework, Spring Boot Web
Favicon
Developing a project using Java Spring Framework, JSON, JPA and PostgreSQL
Favicon
Волшебные скоупы: Как Spring организует работу бинов
Favicon
Quando usar ResponseEntity?
Favicon
What is load balancing and how to do it on client side
Favicon
Capturing and Testing Logs in Java with SLF4J and Logback: A Simple Guide
Favicon
Can you find the Output of this Java Code
Favicon
Ways to Speed Up Spring Boot Application Startup Time
Favicon
Apache wicket with spring boot example application: notice board
Favicon
Handling the "Forwarded" Header in Spring Boot Applications
Favicon
About UriComponentsBuilder and UriComponents
Favicon
Spring Boot: About @SpringBootApplication
Favicon
Spring Security: CSRF protection
Favicon
ISBN Stacks — A look at a possible Spring Application implementation without annotations
Favicon
spring profiles dev production
Favicon
Learn Spring Data JPA, Part - 1
Favicon
Methods for Efficient Large File Processing in Spring Boot
Favicon
Hexagonal Architecture — A Favorite Lyrics Spring Boot — Java Example
Favicon
Validation in Spring REST Framework (SRF)
Favicon
spring
Favicon
Spring Security: Redirect to login page if access pages which is for authorized user only
Favicon
Lambda vs. Named Functions: Choosing the Right Tool for the Job
Favicon
Techniques for Mastering Spring Interceptors: Detailed Guide with Examples
Favicon
Techniques for Using @NoRepositoryBean in Spring Data JPA: A Comprehensive Guide
Favicon
Understanding the Spring Framework: A Developer’s Journey to Clean Code 🚀
Favicon
@PreConstruct and @PostConstruct annotation Spring Boot Example
Favicon
Java’s Functional Programming: the OOP influence
Favicon
🧪 Тестирование с TestRestTemplate и MockMvc: миссия "Котики против багов" 🐞
Favicon
spring boot
Favicon
Reasons to Use WireMock for Testing REST APIs in Spring Boot Applications

Featured ones: