9

I'm using spring applications which sometimes uses @PostConstruct for setup in code and tests

It seems that annotation will be excluded from Java 11:

Note that both @PostConstruct and @PreDestroy annotations are part of Java EE. And since Java EE has been deprecated in Java 9 and removed in Java 11 we have to add an additional dependency to use these annotations

Article suggest replacing all @PostConstruct with afterPropertiesSet method

I recommend you to change implementation from @PostConstruct annotation to implement org.springframework.beans.factory.InitializingBean interface.

Can I blindly replace it in all cases? or are there other considerations?

EDIT

As @JBNizet suggested, this maybe not a must or needed, as Spring doc suggest the opposite

We recommend that you do not use the InitializingBean interface, because it unnecessarily couples the code to Spring. Alternatively, we suggest using the @PostConstruct annotation or specifying a POJO initialization method.

EDIT 2

Another option is using initMethod:

With Java configuration, you can use the initMethod attribute of @Bean

@Bean(initMethod = "init")
public BeanOne beanOne() {
    return new BeanOne();
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 2
    You should just ignore this advice, and keep using PostConstruct. https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle – JB Nizet Apr 07 '19 at 13:10
  • @JBNizet Do you know where's the confusion come from ? there's another reference https://docs.micronaut.io/latest/guide/index.html#java – Ori Marko Apr 07 '19 at 13:13
  • 3
    What "confusion" are you talking about? Neither Micronaut nor Spring are confused. They both use PostConstruct. It's not in the JDK anymore, so what? You add the necessary dependency, as you do for basically everything else. – JB Nizet Apr 07 '19 at 13:15
  • @JBNizet can you answer? There's still an advantages of smaller dependency and size – Ori Marko Apr 07 '19 at 14:10
  • It seems likely that Spring will provide their own `PostConstruct` annotation if and when it is removed from Java. That is assuming that Spring does not already provide their own PostConstruct annotation – DwB Apr 07 '19 at 19:08

2 Answers2

4

Spring uses jakarta.annotation.PostConstruct. As a contributor in spring-cloud-kubernetes, I have used it and included it in that project numerous times. As a matter of fact we favor dropping InitializingBean.

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

Ref https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-postconstruct-and-predestroy-annotations

Like @Resource, the @PostConstruct and @PreDestroy annotation types were a part of the standard Java libraries from JDK 6 to 8. However, the entire javax.annotation package got separated from the core Java modules in JDK 9 and eventually removed in JDK 11. If needed, the javax.annotation-api artifact needs to be obtained via Maven Central now, simply to be added to the application’s classpath like any other library.

Anderson
  • 2,496
  • 1
  • 27
  • 41