Introduction
This is an article where the focus is to show how to use define method as an init method executed after a Java Spring Bean is created. Another one is to show how to define method as a destroy method executed before terminating the Java Spring Bean. This article is referring from several previous articles on the same content and topic. The first one is an article with the title of ‘How to Use Init and Destroy Method in Spring Java Application’ in this link. The second one is an article with the title of ‘How to Define Init and Destroy as Default Method in Spring Java Application’ in this link. Both of the articles are focusing on how to define init and destroy method where the execution is depending of the life cycle of the Java Spring Bean.
Use PostConstruct and PreDestroy Annotation for Defining init and destroy method
So, rather than using the init and destroy method definition in the Java Spring Configuration file, just use the Java Bean Class instead. No need to edit and add attribute of init-method and destroy-method in each Java Spring Bean class definition. As it exist in this article. Moreover, there is also no need to define default-init-method and default-destory-method in the Java Spring Bean root definition. Where it exist as in this article. So, how to achieve the purpose ?. Using the base application exist in the article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link, the following are the steps to achieve it :
-
First of all, just open the Java Spring Bean for an example. In this context, it is the Java Spring Bean with the name of ‘Employee’. The following is the original content :
package com.test.spring; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Employee { private String id; public void setId(String id) { this.id = id; } public void init(){ System.out.println("Employee is going through the init method ... "); } public void destroy(){ System.out.println("Employee is going through the destroy method ... "); } public void identifyID(){ System.out.println("I am an "+this.id+" !"); } }
-
Second, add the necessary annotation to each of the method. Add the @PostConstruct annotation to the init method. On the other hand, add the @PreDestroy annotation to the destroy method as follows :
package com.test.spring; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Employee { private String id; public void setId(String id) { this.id = id; } @PostConstruct public void init(){ System.out.println("Employee is going through the init method ... "); } @PreDestroy public void destroy(){ System.out.println("Employee is going through the destroy method ... "); } public void identifyID(){ System.out.println("I am an "+this.id+" !"); } }
-
Last but not least, just execute the main program for instantiating or creating the Java Spring Bean. The following is the content of the main Java class with the name of ‘SpringMain.java’ :
package com.test.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringMain { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); ((AbstractApplicationContext) context).registerShutdownHook(); Employee employee = (Employee) context.getBean("employeeBean"); employee.setId("Employee of the Company"); employee.identifyID(); } }
It will generate the same output with the one exist in this article and also this article as in the following image :
How to Use PostConstruct and PreDestroy Annotation for defining init and destroy method of Java Spring Bean