spring 自定义bean初始化及析构方法
2015-11-02 20:38
711 查看
package com.test.spring.beans;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Cashier {
private String name;
private String path;
private BufferedWriter writer;
public Cashier() {
System.out.println("*****");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public void openFile() throws FileNotFoundException {
System.out.println("openfile--->");
File logFile = new File(path, name + ".txt");
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile, true)));
}
public void checkout(ShoppingCart cart) throws IOException {
double total =0;
for(Product product: cart.getItems()) {
total += product.getPrice();
}
writer.write(new Date() + "\t" + total + "\r\n");
writer.flush();
}
public void closeFile() throws IOException {
System.out.println("close---->");
writer.close();
}
public static void main(String args[]) throws IOException {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Cashier c = (Cashier) appContext.getBean("cashier");
Product p = new Product("aa",11.1);
List<Product> ps = new ArrayList<Product>();
ps.add(p);
ShoppingCart sc = new ShoppingCart();
sc.setItems(ps);
c.checkout(sc);
System.out.println("checkout---->");
}
}
是通过 init-mehod he destory-method参数指定,init-method指定的方法在构造函数之后执行
<bean id="cashier" class="com.test.spring.beans.Cashier" init-method="openFile" destroy-method="closeFile">
<property name="name" value="cashier"/>
<property name="path" value="D://"/>
</bean>
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Cashier {
private String name;
private String path;
private BufferedWriter writer;
public Cashier() {
System.out.println("*****");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public void openFile() throws FileNotFoundException {
System.out.println("openfile--->");
File logFile = new File(path, name + ".txt");
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile, true)));
}
public void checkout(ShoppingCart cart) throws IOException {
double total =0;
for(Product product: cart.getItems()) {
total += product.getPrice();
}
writer.write(new Date() + "\t" + total + "\r\n");
writer.flush();
}
public void closeFile() throws IOException {
System.out.println("close---->");
writer.close();
}
public static void main(String args[]) throws IOException {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Cashier c = (Cashier) appContext.getBean("cashier");
Product p = new Product("aa",11.1);
List<Product> ps = new ArrayList<Product>();
ps.add(p);
ShoppingCart sc = new ShoppingCart();
sc.setItems(ps);
c.checkout(sc);
System.out.println("checkout---->");
}
}
是通过 init-mehod he destory-method参数指定,init-method指定的方法在构造函数之后执行
<bean id="cashier" class="com.test.spring.beans.Cashier" init-method="openFile" destroy-method="closeFile">
<property name="name" value="cashier"/>
<property name="path" value="D://"/>
</bean>
相关文章推荐
- JAVA相关基础知识
- JAVA DATE解析(时间戳解析为固定格式)
- java方法创建
- Java基础之查找文件名&删除文件夹和文件
- Java 中泛型的全面解析
- java String的使用
- java---表、栈和队列
- java知识体系
- Java 8中的增强的Arrays
- java基础之对properties配置文件的读写操作
- SpringMVC
- SpringIOC_容器的类扫描注解
- spring 读取操作系统上的文件
- java怎么实现两个对象内容的交换
- Java list容器
- Eclipse 编写java时, 出现的库访问限制问题
- JAVA 数组作业——动手动脑以及课后实验性问题
- Eclipse下Java链接MySQL数据库
- 关于Java交换两个对象的问题
- java线程基础知识和小案例