Bean Lifecycle: @PostConstruct and @PreDestroy
Learn about the bean lifecycle methods annotated with @PostConstruct and @PreDestroy.
We'll cover the following
Spring manages the entire lifecycle of beans from the time they are created till the time they are destroyed. It provides post-initialization and pre-destruction callback methods on the beans. The developer can tap into these callbacks and write custom initialization and cleanup code.
For the code example shown in this lesson, we have created a sub-package called lesson11
inside the package io.datajek.spring.basics.movierecommendersystem
. The package contains MovieRecommenderSystemApplication.java
, Filter.java
, ContentBasedFilter.java
, and RecommenderImplementation.java
files.
In this lesson, we will introduce the slf4j
logger to log the sequence of events in our application and use it instead of System.out.println
. Loggers offer flexibility, as well as better output, that includes timestamp, name of the thread executing the code, and the name of the class. This information comes in handy when debugging applications. We will declare a logger and import the relevant files as follows:
Get hands-on with 1400+ tech skills courses.