该文章的代码运行可以参照https://blog.csdn.net/weixin_43908036/article/details/89059612,在其基础上调试运行。
depends-on是指指定Bean初始化及销毁时的顺序,使用depends-on属性指定的Bean要先初始化完毕后才初始化当前Bean,使用 depends-on属性指定的Bean要在指定的Bean之后销毁。例如:
<bean id="helloApi" class="com.demo.ioc.HelloImpl"/>
<bean id="decorator"
class="com.demo.ioc.HelloApiDecorator"
depends-on="helloApi">
<property name="helloApi"><ref bean="helloApi"/></property>
</bean>
“decorator”指定了“depends-on”属性为“helloApi”,所以在“decorator”Bean初始化之前要先初始化 “helloApi”,而在销毁“helloApi”之前先要销毁“decorator”。
“depends-on”的好处主要是给出明确的初始化及销毁顺序,让我们来看个例子,在平常开发中我们需要访问文件系统,而文件打开、关闭是必须配对的,不能打开后不关闭,从而造成其他程序不能访问该文件,具体代码如下:
1)准备测试类:ResourceBean和DependentBean
其中,ResourceBean从配置文件中配置文件位置,然后定义初始化方法init中打开指定的文件,然后获取文件流。
package com.demo.ioc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ResourceBean {
private FileOutputStream fos;
private File file;
//初始化方法
public void init(){
System.out.println("ResourceBean:===============初始化");
//加载资源,这里只是演示
System.out.println("ResourceBean:===============加载资源,执行一些预操作");
try {
this.fos=new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//销毁资源方法
public void destroy(){
System.out.println("ResourceBean:===============销毁");
//释放资源
System.out.println("ResourceBean:===============释放资源,执行一些清理操作");
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public FileOutputStream getFos() {
return fos;
}
public void setFos(FileOutputStream fos) {
this.fos = fos;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
定义销毁方法destroy用于在应用程序关闭时调用该方法,关闭文件流。DependentBean中会注入ResourceBean,并从ResourceBean中获取文件流写入内容;定义init方法来定义一些初始化操作并向文件中输出文件头信息;最后定义销毁方法用于在关闭应用程序的时候向文件中输出文件尾信息。
package com.demo.ioc;
import java.io.IOException;
public class DependentBean {
ResourceBean resourceBean;
public void write(String ss) throws IOException {
System.out.println("DependentBean:===============写资源");
resourceBean.getFos().write(ss.getBytes());
}
//初始化方法
public void init() throws IOException {
System.out.println("DependentBean:===============初始化");
resourceBean.getFos().write("DependentBean:===============初始化".getBytes());
}
//销毁方法
public void destroy() throws IOException {
System.out.print("DependentBean:===============销毁");
//在销毁之前需要往文件中写销毁内容
resourceBean.getFos().write("DependentBean:===============销毁".getBytes());
}
public void setResourceBean(ResourceBean resourceBean){
this.resourceBean=resourceBean;
}
}
2)Bean定义
<!--延迟初始化bean,使用depends-on-->
<bean id="resourceBean" class="com.demo.ioc.ResourceBean" init-method="init" destroy-method="destroy">
<property name="file" value="D:/test.txt"/>
</bean>
<bean id="dependentBean"
class="com.demo.ioc.DependentBean"
init-method="init" destroy-method="destroy" depends-on="resourceBean">
<property name="resourceBean" ref="resourceBean"/>
</bean>
:Spring容器能自动把字符串转换为java.io.File。init-method=“init” 指定初始化方法,setterdestroy-method="destroy"指定销毁方法,在此配置中,resourceBean初始化在dependentBean之前被初始化,resourceBean销毁会在dependentBean销毁之后执行。
3)测试代码
package com.demo.ioc;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MoreDependencyInjectTest {
@Test
public void testDependOn() throws IOException {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("Bean.xml");
//要注册销毁回调,否则我们定义的销毁方法不执行
context.registerShutdownHook();
DependentBean dependentBean=context.getBean("dependentBean",DependentBean.class);
dependentBean.write("aaa");
}
}
4)执行结果