spring 自定义bean初始化及析构方法
来源:互联网 发布:十二宫杀手解析知乎 编辑:程序博客网 时间:2024/06/06 09:27
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---->");
}
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>
0 0
- spring 自定义bean初始化及析构方法
- spring之 自定义Bean初始化和析构
- Spring初始化Bean方法
- Spring入门学习——自定义Bean初始化和析构
- 借助@Conditional实现自定义初始化Spring Bean
- [Spring]Spring Bean的初始化 ---- getBean方法
- spring两种初始化bean的方法
- Spring中bean的初始化方法
- spring bean初始化和销毁方法
- Spring bean初始化和销毁方法
- Spring Bean初始化之后执行指定方法
- Spring中bean初始化时机初始化方法和销毁方法
- 2--spring bean 的作用域scope 设置;bean的初始化和销毁方法,profile配置不同环境实例化不同的bean,自定义事件,自定义监听器
- spring bean初始化及销毁你必须要掌握的回调方法。
- 随spring context初始化完成后init自定义bean
- spring详解:通过FactoryBean自定义工厂初始化Bean
- Spring——Bean的初始化方法和销毁方法
- spring boot 管理bean初始化方法,以及销毁方法
- Centos彻底删除mysql
- JAVA相关基础知识
- 查找无序数组中的第K个最小值[快排思想]
- centos pil
- 高效开发Android App的10个建议
- spring 自定义bean初始化及析构方法
- 技术人员硬实力和软实力
- Objective-C中的Protocol
- codevs1928 加密算法 数论[四星]
- 【HNOI2012】【BZOJ2732】射箭
- 解决android中软件盘弹出导致的控件位置问题
- C++学习——copy构造函数及浅拷贝和深拷贝
- qt creator 在linux系统加载动态连接库
- 解压