项目结构如下:

ResourceBean.java代码:

 package com.it.res;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class ResourceBean { private FileOutputStream out;
private File file; public void init(){
System.out.println("初始化的方法!!!!!--->加载资源");
try {
this.out = new FileOutputStream(file); } catch (FileNotFoundException e) {
e.printStackTrace();
}
} public void destroy(){
System.out.println("销毁的方法!!!--->清理内存");
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} public FileOutputStream getOut(){
return out;
} public void setFile(File file){
this.file=file;
}
}

DependentBean.java代码:

 package com.it.res;

 import java.io.IOException;

 public class DependentBean {
ResourceBean bean;
public void write(String ss){
System.out.println("写入资源");
try {
bean.getOut().write(ss.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} public void init(){
System.out.println("depen--->初始化");
try {
bean.getOut().write("depen---->初始化".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} public void destroy(){
System.out.println("depen--->销毁");
try {
bean.getOut().write("depen--->销毁".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} public ResourceBean getBean() {
return bean;
} //设置注入
public void setBean(ResourceBean bean) {
this.bean = bean;
} }

resWrite.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- init-method 指定初始化方法,在构造器注入和setter注入完毕后执行。 destroy-method 只有“singleton”作用域能销毁,“prototype”作用域的一定不能,其他作用域不一定能 lazy-init="true"表示懒加载,不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean-->
<bean id="resourceBean" class="com.it.res.ResourceBean" init-method="init" destroy-method="destroy" lazy-init="true">
<property name="file" value="F:/test/a.txt"> </property><!-- Spring容器能自动把字符串转换为java.io.File -->
</bean> <!-- 指定depends-on 则resourceBean会在dependentBean之前初始化,在dependentBean销毁之后销毁-->
<bean id="dependentBean" class="com.it.res.DependentBean" init-method="init" destroy-method="destroy" depends-on="resourceBean">
<property name="bean" ref="resourceBean"></property>
</bean> </beans>

测试类Test.java代码:

 package com.it.res;

 import org.springframework.context.support.FileSystemXmlApplicationContext;

 public class Test {

     @org.junit.Test
public void testDepen(){
FileSystemXmlApplicationContext app = new FileSystemXmlApplicationContext("resources/resWrite.xml");
//一定要注册销毁回调,否则我们定义的销毁方法不执行
app.registerShutdownHook();
DependentBean bean = (DependentBean) app.getBean("dependentBean");
bean.write("\r\n德玛西亚\r\n");
}
}

测试完成,控制打印如下:

文件写入:

最新文章

  1. 用Myisamchk让MySQL数据表更健康
  2. 【BZOJ 1758】【WC 2010】重建计划 分数规划+点分治+单调队列
  3. jafka消息结构
  4. 9月java货车版速记
  5. MVC 介绍
  6. 使用resumable.js上传大文件(视频)兵转换flv格式
  7. Codeforces 220B - Little Elephant and Array 离线树状数组
  8. cocos项目导入其它源文件时加入依赖库时,头文件提示找不到文件夹中的文件
  9. Test SRM Level Three: LargestCircle, Brute Force
  10. Web安全知多少
  11. nodeJS之流stream
  12. Spring Security 入门(1-9)国际化的使用
  13. 【一天一道LeetCode】#326. Power of Three
  14. Python03(Linux和Python简介)
  15. v2ray和ss的安装资料整理
  16. 通用Excel文件导出工具类
  17. 2.Swift快速浏览
  18. 性能测试-10.数据分析Analysis
  19. day13(反射,BeanUtils包)
  20. SQL SERVER2008 存储过程、表、视图、函数的权限

热门文章

  1. Solr集群常用的操作总结
  2. Divide and conquer:Drying(POJ 3104)
  3. Android屏幕适配dp、px两套解决办法
  4. 【C语言】pragma
  5. 20145213《Java程序设计学习笔记》第六周学习总结
  6. GCD的使用
  7. ERROR ITMS-90032 “Invalid image path”
  8. ios 上下拉刷新
  9. 苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推
  10. sql语句的join用法