Spring @PostConstruct and @PreDestroy alternatives in Java 9+

Spring @PostConstruct and @PreDestroy alternatives in Java 9+

Both @PostConstruct and @PreDestroy annotations (often used in spring applications) are located in java.xml.ws.annotation package which is a part of  Java EE module. J2EE has been deprecated in Java 9 and it’s scheduled to be removed in Java 11. Let’s look how to run your application without errors again in two approaches: with and without code modification.

Approach 1: With code modification

The problem mentioned in the topic becomes easier to solve when you are able to modify an application code.

There is several ways you can achieve similar behaviour like @PostConstruct annotation. Most of them are described here. I recommend you to change implementation from @PostConstruct annotation to implement  org.springframework.beans.factory.InitializingBean interface.

Instead of the  @PreDestroy annotation I recommend you to implement destroy method from org.springframework.beans.factory.DisposableBean interface. Let’s see what you have to change based on the example below:

 

Below is what the code looks like after modifications:

As you can see you have to just implement  afterPropertiesSet  from the  InitializingBean  interface and call the init() method inside it. Instead of   @PreDestroy annotation you have to implement  destroy method from  DisposableBean and call the shutdown method in it. After all remove unused @PostConstruct  and   @PreDestroy annotations.

 

Approach 2: Without code modification

There are three methods I have found to get both  @PostConstruct and @PreDestroy annotations still working in Java 9 and newer.

Metod 1: Add necessary dependencies to pom.xml or build.gradle

This is the most recommended method. Especially if you don’t want to modify existing code and you are able to add missing  javax.annotation-api  dependency. Blow you can find both pom.xml and build.gradle  examples. The newest version of jar you can find here.

 

 

Method 2: Download dependencies manually and change the application run command

The idea behind this method is similar like in previous one. You have to download missing javax.annotation-api jar manually and add it to the classpath.

If you want to run jar file, you have to change run command from java -jar to java -cp like below:

 

Method 3: Add  java.se.ee module

This method is not recommended because it won’t be working in JDK 11. As I have mentioned before  java.xml.ws.annotation is deprecated to remove in JDK 11. But you can use this method temporary for both JDK 9 and JDK 10.  Only you have to do is to add  java.se.ee module to the JVM by using  --add-modules switch. Below is an example of application invocation.

Before:

After

 

If you think this post is valuable, please leave me +1  or share it. This action will allow me to reach a wider audience.

Thank you.

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu