【spring bean】spring中bean的懒加载和depends-on属性设置
项目结构如下:

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属性设置的更多相关文章
- 【Spring源码分析】非懒加载的单例Bean初始化过程(下篇)
doCreateBean方法 上文[Spring源码分析]非懒加载的单例Bean初始化过程(上篇),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下 ...
- 【Spring源码分析】非懒加载的单例Bean初始化前后的一些操作
前言 之前两篇文章[Spring源码分析]非懒加载的单例Bean初始化过程(上篇)和[Spring源码分析]非懒加载的单例Bean初始化过程(下篇)比较详细地分析了非懒加载的单例Bean的初始化过程, ...
- Spring源码分析:非懒加载的单例Bean初始化前后的一些操作
之前两篇文章Spring源码分析:非懒加载的单例Bean初始化过程(上)和Spring源码分析:非懒加载的单例Bean初始化过程(下)比较详细地分析了非懒加载的单例Bean的初始化过程,整个流程始于A ...
- Spring源码分析:非懒加载的单例Bean初始化过程(下)
上文Spring源码分析:非懒加载的单例Bean初始化过程(上),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireC ...
- 018 关联映射文件中<class>标签中的lazy(懒加载)属性
Lazy(懒加载): 只有在正真使用该对象时,才会创建这个对象 Hibernate中的lazy(懒加载): 只有我们在正真使用时,它才会发出SQL语句,给我们去查询,如果不使用对象则不会发SQL语句进 ...
- vue项目中实现图片懒加载的方法
对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载.这样子对于页面加载性能上会有很大的提升,也提高了用户体验. 实 ...
- Vue项目中实现图片懒加载
个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! ---对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面 ...
- 【Spring源码分析】非懒加载的单例Bean初始化过程(上篇)
代码入口 上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了f ...
- Spring源码分析:非懒加载的单例Bean初始化过程(上)
上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了finish ...
- Spring IOC - 控制反转(依赖注入) - 懒加载机制
懒加载机制 Spring默认会在容器初始化的过程中,解析xml,并将单例的bean创建并保存到map中,这样的机制在bean比较少的时间问题不大,但一旦bean非常多时,Spring需要在启动的过程中 ...
随机推荐
- ui router 介绍
1. 路由规则 rap框架页面路由基于ui-router实现 1.1 ui-router 一个基本的路由状态如下所示: 路由配置: $stateProvider .state('po',{ url:' ...
- apache的httpd.conf翻译
# This is the main Apache HTTP server configuration file. It contains the 这是Apache HTTP Server的主配置文件 ...
- ffmpeg-20160506-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- Effective C++ -----条款53:不要轻忽编译期的警告
严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取“无任何警告”的荣誉. 不要过度依赖编译器的报警能力,因为不同的编译器对待事情的态度并不相同.一旦移植到另一个编译器上,你元 ...
- xp系统打开软件程序总是弹出警告窗口,很烦人对不,怎么办呢?进来看
为了不浪费比较着急的朋友的的时间,先把解决方案说了,下面我在细说: 细说: 今天装了个xp的虚拟机,为了不在xp里重复装真机(win7的)里已经有的软件,就把我的工具盘共享给了虚拟机,大部分软件都可以 ...
- 【XLL API 函数】xlCoerce
将 XLOPER/XLOPER12 转换为另一种类型,或是查询表格中的单元格值. 函数原型 Excel12(xlCoerce, LPXLOPER12 pxRes, 2, LPXLOPER12 pxSo ...
- jQuery取复选框值、下拉列表里面的属性值、取单选按钮的属性值、全选按钮、JSON存储、*去空格
1.jquery取复选框的值<!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></scri ...
- Jquery的鼠标移动上去显示div,鼠标离开的时候隐藏div效果
有时候我们需要这个效果:当鼠标放上去的时候显示一个div,当鼠标移走的时候就将div隐藏了.代码如下,记得引入Jquyer库.(当鼠标移动到id=menu的div上的时候,显示id=list的div, ...
- Hibernate单向一对多对象关系模型映射
1 hibernate 的对象关系映射 Orm: 类-----表 属性------字段 对象------记录 表:在数据库中存在主外键的关系,反向工厂类是由表生成,在由表生成类的时候,类和类之间存在者 ...
- Linear regression with multiple variables(多特征的线型回归)算法实例_梯度下降解法(Gradient DesentMulti)以及正规方程解法(Normal Equation)
,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, , ...