java / intermediate
Snippet
Lifecycle Management with @PostConstruct
The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This is part of the Spring bean lifecycle.
snippet.java
1
2
3
4
5
6
7
@Componentpublic class DataSeeder {@PostConstructpublic void init() {System.out.println("Database seeding started after bean construction.");}}
spring
Breakdown
1
@PostConstruct
Indicates that the method should run immediately after the bean is initialized by Spring.
2
public void init()
The method must have a void return type and take no arguments.