Introduction
This is an article where the main focus is just to define a default method to initialize and destroy all the Java Spring Bean available in the Spring Java Application. There are some articles which has the strong relation with the content of this article. The first one is the article with the title of ‘How to Use Init and Destroy Method in Spring Java Application’ in this link. That article is the main reference for the explanation of using the init and destroy method. But in this article, the usage of init and destroy is only for one Java Spring Bean. This article will show how to use the init and destroy method not only for just one Java Spring Bean but also for the entire Java Spring Bean available in the Spring Java Application.
How to Define Init and Destroy as Default Method in Spring Java Application
The method for defining the life cycle of a Java Spring Bean which is the init and the destroy method. The base for explaining the init and destroy method also exist in the previous article. That article’s title is ‘How to Create a Simple Java Spring Application using NetBeans IDE’ and it exist in this link. So, what is the steps for defining the init and destroy method as the default method for all the Java Spring Bean ?. Just take a look below :
-
First of all, just open the Java Spring Configuration file. That file exist in the project precisely in ‘src/main/resources’. Furthermore, the file name is ‘spring-config.xml’. The original content before the modification exist below :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="employeeBean" class="com.test.spring.Employee" init-method="init" destroy-method="destroy"> </bean> </beans>
-
Next, modify the configuration above by changing it into the following content :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="init" default-destroy-method="destroy"> <bean id="employeeBean" class="com.test.spring.Employee"> </bean> </beans>
So, the modification is actually clearly enough in the last piece of snippet code of the configuration. Instead of giving the ‘init-method’ and ‘destroy-method’ to each of all the Java Spring Bean definition which is going to cause a lot of effort, just add it to the main ‘beans’ tag. In that case, erase all of the attribute ‘init-method’ and ‘destroy-method’ in all of the bean tag definition. Change it by adding the parameter ‘default-init-method’ and ‘default-destroy-method’ in the beans tag to implement it as default to all of the Java Spring Bean definition. It will actually produce the same result as follows :
How to Define Init and Destroy as Default Method in Spring Java Application