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