I have a lot of singleton beans defined in my spring application context both different objects of the same type and objects of different types.
To do some pre-destroy operations, I have implemented the DisposableBean
interface for the bean classes. But, I would like to know how to destroy a bean with a particular id.
Here is my destroybean.xml
<?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="st" class="spring17.Student">
<property name="sno" value="101"/>
<property name="sname" value="Smith"/>
<property name="age" value="20"/>
</bean>
<bean id="st1" class="spring17.Student">
<property name="sno" value="102"/>
<property name="sname" value="Scott"/>
<property name="age" value="22"/>
</bean>
</beans>
Here is my main class
package spring17;
import org.springframework.context.support.GenericXmlApplicationContext;
public class SpringPrg {
@SuppressWarnings("resource")
public static void main(String args[])
{
GenericXmlApplicationContext gc=new GenericXmlApplicationContext("classpath:destroybean.xml");
Student st=gc.getBean("st",Student.class);
System.out.println(st);
gc.destroy();
}
}
When i call the gc.destroy()
st1
is also being destroyed which I don't want. You may suggest adding lazy-init
attribute for st1
but it is not what I wanted.
Thanks in advance.