The spring framework allows to configure method which will be called before bean destroy and to perform some destruction logic for specified spring bean ie. releasing resources etc. It will execute the specified method just before bean is destroy. Once the specified destroy method executed, it will be ready to destroy.
When bean destroy method is configure in application, It will be called every time just before bean gets destroy by the spring IoC container. It depends on spring bean scope, if bean define with singleton scope, it will executes only once. In case of prototype, it will be called every time when bean gets destroy.
Ways to configure destroy method for spring bean
1. | Bean xml configuration |
2. | Annotation based configuration |
3. | Implementing DisposableBean interface |
Bean xml configuration
1 package com.iogyan.service;
2
3 public class AudiService {
4
5 public String getName() {
6 return "Audi";
7 }
8
9 public void destroy() {
10 System.out.println("Audi service class destroy method called..");
11 }
12 }
In the above example, a class AudiService is created with methods getName() that returns name of the service and destroy() prints message on console when called using class instance.
Spring bean configuration
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7
8 <bean id="audiService" class="com.iogyan.service.AudiService" destroy-method="destroy"/>
9 </beans>
In the above configuration, is created by specifying a bean definition with three properties id, class and destroy-method by specifying a name of method that will be called when bean of class AudiService is destroyed.
Validating destroy method using java application
1 package com.iogyan.example;
2
3 import org.springframework.context.support.ClassPathXmlApplicationContext;
4 import com.iogyan.service.AudiService;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10
11 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
12 context.setConfigLocation("applicationContext.xml");
13 context.refresh();
14
15
16 AudiService audi = (AudiService) context.getBean("audiService");
17 System.out.println(audi.getName());
18
19
20 context.close();
21 }
22 }
In the above example, a ClassPathXmlApplicationContext is created and set configuration file in the context using setConfigLocation() method. A context method getBean() called by specifying an ID of bean. A getName() method called that prints the name of service. A context close() method is called that destroy the container bean which will call destroy() method of AudiService.
1 Audi
2 Audi service class destroy method called..
Annotation based configuration
Service class with annotation
1 package com.iogyan.service;
2
3 import javax.annotation.PreDestroy;
4 import org.springframework.stereotype.Service;
5
6 @Service
7 public class AudiService {
8
9 public String getName() {
10 return "Audi";
11 }
12
13 @PreDestroy
14 public void destroy() {
15 System.out.println("Audi service class destroy method called..");
16 }
17 }
In the above example, a AudiService class is created that annotated with @Service annotation and two method getName() and destroy() method. A destroy() method annotated with @PreDestroy annotation which will be called just before a bean is destroyed. It prints message on console.
1 package com.iogyan.example;
2
3 import org.springframework.context.annotation.ComponentScan;
4
5 @ComponentScan(basePackages = "com.iogyan.service")
6 public class ApplicationConfiguration {
7
8 }
In the above example, a class ApplicationConfiguration created by annotating with @ComponentScan() annotation by specifying a name of package. It is similar to component-scan tag in xml based configuration. It creates spring bean of class which are annotated, if bean id not specified in the annotation, a default ID will be same as class name where first character of the name will be in lowercase. ie. for AudiService, bean id will be audiSerivce.
Validating destroy method using java application
1 package com.iogyan.example;
2
3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4 import com.iogyan.service.AudiService;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10
11 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
12
13 context.register(ApplicationConfiguration.class);
14 context.refresh();
15
16
17 AudiService tata = (AudiService) context.getBean("audiService");
18 System.out.println(tata.getName());
19
20
21 context.close();
22 }
23 }
In the above code, an instance of AnnotationConfigApplicationContext is created using constructor method. A context method register() method called by specifying a name of ApplicationConfiguration class and refresh() the bean definitions in spring container. A context method getBean() method called by specifying an ID of spring bean. It returns bean of AudiService, a method getName() is called returns name of service. A context close() method called that close the context but before a bean is destroyed, it executes bean destroy() method.
1 Audi
2 Audi service class destroy method called..
Implementing DisposableBean interface
Service class by implementing DisposableBean interface
1 package com.iogyan.service;
2
3 import org.springframework.beans.factory.DisposableBean;
4 import org.springframework.stereotype.Service;
5
6 @Service
7 public class AudiService implements DisposableBean {
8
9 public String getName() {
10 return "Audi";
11 }
12
13 @Override
14 public void destroy() {
15 System.out.println("Audi service class destroy method called..");
16 }
17 }
In the above example, a class AudiService class is created by implementing an DisposableBean interface and overriding a destroy() method. It will be called when a bean of class AudiService is destroyed.
Java application configuration
1 package com.iogyan.example;
2
3 import org.springframework.context.annotation.ComponentScan;
4
5 @ComponentScan(basePackages = "com.iogyan.service")
6 public class ApplicationConfiguration {
7
8 }
In the above example, a class ApplicationConfiguration created by annotating with @ComponentScan() annotation by specifying a name of package. It is similar to component-scan tag in xml based configuration. It creates spring bean of class which are annotated, if bean id not specified in the annotation, a default ID will be same as class name where first character of the name will be in lowercase. ie. for AudiService, bean id will be audiSerivce.
Validating destroy method using java application
1 package com.iogyan.example;
2
3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4 import com.iogyan.service.AudiService;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10
11 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
12
13 context.register(ApplicationConfiguration.class);
14 context.refresh();
15
16
17 AudiService tata = (AudiService) context.getBean("audiService");
18 System.out.println(tata.getName());
19
20
21 context.close();
22 }
23 }
In the above code, an instance of AnnotationConfigApplicationContext is created using constructor method. A context method register() method called by specifying a name of ApplicationConfiguration class and refresh() the bean definitions in spring container. A context method getBean() method called by specifying an ID of spring bean. It returns bean of AudiService, a method getName() is called returns name of service. A context close() method called that close the context. If destroy post processor define for bean, it will executes.
1 Audi
2 Audi service class destroy method called..
Related options for your search