Annotation Interface PostConstruct
The PostConstruct
annotation is used on a method that needs
to be executed after dependency injection is done to perform any
initialization. This method is called immediately after the bean's
properties have been set and the bean has been placed into the
Bring container.
Methods annotated with @PostConstruct
are invoked only once
in the bean's lifecycle, and they provide a convenient way to
initialize resources or perform any setup logic that is required
before the bean is ready for use.
The method annotated with @PostConstruct
must be non-static
and should not have any parameters, as it is meant to be an
initialization callback method for the bean instance. If multiple
methods are annotated with @PostConstruct
within a single
class, the order of execution is not guaranteed.
Example:
import com.bobocode.bring.core.annotation.PostConstruct;
public class ExampleBean {
private String message;
@PostConstruct
public void init() {
message = "Hello, this is an example!";
// Additional initialization logic
}
public String getMessage() {
return message;
}
}
In this example, the init
method will be automatically
invoked after the ExampleBean
is constructed, providing a
way to perform custom initialization logic.
- Since:
- 1.0
- Author:
- Blyzhnytsia Team