Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, November 22, 2007

java code for Destroy Method

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/


///////////////////////////////////////////////////////////////////////////////////////
//File: disposeMethod.xml





c:/test.txt



///////////////////////////////////////////////////////////////////////////////////////

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class DestructiveBeanWithInterface implements InitializingBean,
DisposableBean {

private InputStream is = null;

public String filePath = null;

public void afterPropertiesSet() throws Exception {

System.out.println("Initializing Bean");

if (filePath == null) {
throw new IllegalArgumentException(
"You must specify the filePath property of " + DestructiveBean.class);
}

is = new FileInputStream(filePath);
}

public void destroy() {

System.out.println("Destroying Bean");

if (is != null) {
try {
is.close();
is = null;
} catch (IOException ex) {
System.err.println("WARN: An IOException occured"
+ " trying to close the InputStream");
}
}
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
///////////////////////////////////////////////////////////////////////////////////////
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class DestructiveBean implements InitializingBean {

private InputStream is = null;

public String filePath = null;

public void afterPropertiesSet() throws Exception {

System.out.println("Initializing Bean");

if (filePath == null) {
throw new IllegalArgumentException(
"You must specify the filePath property of " + DestructiveBean.class);
}

is = new FileInputStream(filePath);
}

public void destroy() {

System.out.println("Destroying Bean");

if (is != null) {
try {
is.close();
is = null;
} catch (IOException ex) {
System.err.println("WARN: An IOException occured"
+ " trying to close the InputStream");
}
}
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public static void main(String[] args) throws Exception {
ConfigurableListableBeanFactory factory = new XmlBeanFactory(
new FileSystemResource(
"build/disposeMethod.xml"));


DestructiveBean bean = (DestructiveBean) factory.getBean("destructiveBean");

System.out.println("Calling destroySingletons()");
factory.destroySingletons();
System.out.println("Called destroySingletons()");
}
}

No comments: