项目结构如下:

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");
}
}

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

文件写入:

【spring bean】spring中bean的懒加载和depends-on属性设置的更多相关文章

  1. 【Spring源码分析】非懒加载的单例Bean初始化过程(下篇)

    doCreateBean方法 上文[Spring源码分析]非懒加载的单例Bean初始化过程(上篇),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下 ...

  2. 【Spring源码分析】非懒加载的单例Bean初始化前后的一些操作

    前言 之前两篇文章[Spring源码分析]非懒加载的单例Bean初始化过程(上篇)和[Spring源码分析]非懒加载的单例Bean初始化过程(下篇)比较详细地分析了非懒加载的单例Bean的初始化过程, ...

  3. Spring源码分析:非懒加载的单例Bean初始化前后的一些操作

    之前两篇文章Spring源码分析:非懒加载的单例Bean初始化过程(上)和Spring源码分析:非懒加载的单例Bean初始化过程(下)比较详细地分析了非懒加载的单例Bean的初始化过程,整个流程始于A ...

  4. Spring源码分析:非懒加载的单例Bean初始化过程(下)

    上文Spring源码分析:非懒加载的单例Bean初始化过程(上),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireC ...

  5. 018 关联映射文件中<class>标签中的lazy(懒加载)属性

    Lazy(懒加载): 只有在正真使用该对象时,才会创建这个对象 Hibernate中的lazy(懒加载): 只有我们在正真使用时,它才会发出SQL语句,给我们去查询,如果不使用对象则不会发SQL语句进 ...

  6. vue项目中实现图片懒加载的方法

    对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载.这样子对于页面加载性能上会有很大的提升,也提高了用户体验. 实 ...

  7. Vue项目中实现图片懒加载

    个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! ---对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面 ...

  8. 【Spring源码分析】非懒加载的单例Bean初始化过程(上篇)

    代码入口 上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了f ...

  9. Spring源码分析:非懒加载的单例Bean初始化过程(上)

    上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了finish ...

  10. Spring IOC - 控制反转(依赖注入) - 懒加载机制

    懒加载机制 Spring默认会在容器初始化的过程中,解析xml,并将单例的bean创建并保存到map中,这样的机制在bean比较少的时间问题不大,但一旦bean非常多时,Spring需要在启动的过程中 ...

随机推荐

  1. DATEADD和DATEDIFF函数、其他日期处理方法 、已打开的端口、FORMAT函数

    DATEADD和DATEDIFF函数.其他日期处理方法 .已打开的端口.FORMAT函数 DATEADD和DATEDIFF函数.其他日期处理方法 .已打开的端口.Format函数 KeyLife富翁笔 ...

  2. Java for LeetCode 221 Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  3. HTML超链接

    打开网页在 想要查看的位置右键单击   审查元素  则可以查看代码    点击图片右键单独打开  则可以查看图片位置 一.超链接 a标签   <a href="地址"> ...

  4. 在微信浏览器中如何让他自动关闭当前页面回到会话框js

    <script type="text/javascript"> wx.config(jssdkconfig); require(['jquery', 'util'], ...

  5. Excel去重

    在excel2007中,数据——>数据工具——>删除重复项也可使用高级筛选:数据——>排序和筛选中的高级——>弹出高级筛选对话框,设置列表区域和条件区域,并勾选“选择不重复记录 ...

  6. [Android Pro] Android 4.3 NotificationListenerService使用详解

    reference to : http://blog.csdn.net/yihongyuelan/article/details/40977323 概况 Android在4.3的版本中(即API 18 ...

  7. 备忘zookeeper(单机+伪集群+集群)

    #下载: #单机模式 解压到合适目录. 进入zookeeper目录下的conf子目录, 复制zoo_sample.cfg-->zoo.cfg(如果没有data和logs就新建):tickTime ...

  8. C#资源文件管理

    1.右键项目点属性; 2.点资源项,添加资源下拉框的添加现在文件,如下图: 3.直接上代码获取并复制到指定文件夹下: private void button1_Click(object sender, ...

  9. C 工厂模式 还有其他的模式

    http://blog.csdn.net/feixiaoxing/article/details/7081243

  10. NYOJ题目64鸡兔同笼

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsAAAAIZCAIAAAAnfB5fAAAgAElEQVR4nO3dO1LjygIG4LsJchZC7I ...