Home > Software engineering >  Callbacks in bean lifecycle
Callbacks in bean lifecycle

Time:01-27

I was just going through spring documentation for Lifecycle callbacks for a bean. For initialization callback the documentation recommends using @PostConstruct annotation instead of using the InitializingBean interface implementation as it couples code with Spring. Could someone please explain what they mean when they say coupling code with spring. I will share the link to the section i was going through here.

CodePudding user response:

InitializingBean is pure Spring and will only work in Spring. @PostConstruct is ordinary Java 1.8. Sadly, from 9 it's dependent on javax.annotation:javax.annotation-api:1.3.2

CodePudding user response:

The @PostContruct annotation is not Spring-specific, but from Jakarta Annotations. This way, your code is not tied to Spring specifically, but could also work in a non-Spring environment.

Here is the relevant Javadoc from the Jakarta documentation: PostConstruct.

  • Related