depends-on的作用
depends-on的作用一般是一个bean依赖于另外一个bean,被依赖的bean一般用于一些初始化和收尾的工作
如在这个例子中,DependentBean依赖于ResourceBean,ResourceBean主要用于指定文件路径、打开文件、和关闭文件流
而DependentBean只负责写文件,这样更能体现单一职责
另外需要关注两个bean的init和destory的执行顺序
先执行被依赖bean的init方法,然后执行依赖bean的方法
先执行依赖bean的destory方法,后执行被依赖bean的destory方法
/**
*
*
* <pre>HISTORY
* ****************************************************************************
* ID DATE PERSON REASON
* 1 2018年1月17日 Simba.Hua Create
* ****************************************************************************
* </pre>
* @author Simba.Hua
*/
public class DependentBean {
private ResourceBean resourceBean;
public void write(String str) throws IOException {
System.out.println("DependentBean写资源" + str);
resourceBean.getFos().write(str.getBytes());
}
public void init() throws IOException {
System.out.println("DependentBean:初始化");
resourceBean.getFos().write("DependentBean:初始化\r\n".getBytes());
}
public void destroy() throws IOException {
System.out.println("DependentBean:销毁资源");
resourceBean.getFos().write("DependentBean:销毁资源\r\n".getBytes());
}
public ResourceBean getResourceBean() {
return resourceBean;
}
public void setResourceBean(ResourceBean resourceBean) {
this.resourceBean = resourceBean;
}
}
package com.shux.springsource.depends.on;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 描述:测试spring中,depends-on的作用
*
*
* <pre>HISTORY
* ****************************************************************************
* ID DATE PERSON REASON
* 1 2018年1月17日 Simba.Hua Create
* ****************************************************************************
* </pre>
* @author Simba.Hua
*/
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;
}
}
<!-- 测试depends-on -->
<bean id="resourceBean" class="com.shux.springsource.depends.on.ResourceBean" init-method="init" destroy-method="destroy">
<property name="file" value="D:/test.txt"></property>
</bean>
<bean id="dependentBean" class="com.shux.springsource.depends.on.DependentBean" init-method="init" destroy-method="destroy" depends-on="resourceBean">
<property name="resourceBean" ref="resourceBean"></property>
</bean>
@Test
public void testDependsOn() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mybeans.xml");
context.registerShutdownHook();
DependentBean dependentBean = context.getBean("dependentBean",DependentBean.class);
try {
dependentBean.write("hello,是我正在写入哦\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
look-up
look-up 用于解决当一个单例的bean的属性中存在一个prototype的属性bean,如果用set,get注入
* 则prototype的属性也会跟单例bean的生命周期一样,即属性bean只会产生一个实例bean,这不是我们想要的,
* 我们想要的是可以产生不同的实例,而look-up就是解决这一问题的,用look-up即使宿主bean是单例的,只要属性bean是
* prototype类型的,就可以产生多个实例.
如下例子,例子来自http://jinnianshilongnian.iteye.com/blog/1415461
package com.shux.springsource.lookup;
/**
* 描述:
*
* <pre>HISTORY
* ****************************************************************************
* ID DATE PERSON REASON
* 1 2018年1月18日 Simba.Hua Create
* ****************************************************************************
* </pre>
* @author Simba.Hua
*/
public interface HelloApi {
void sayHello();
}
package com.shux.springsource.lookup;
/**
* 描述:
*
* <pre>HISTORY
* ****************************************************************************
* ID DATE PERSON REASON
* 1 2018年1月18日 Simba.Hua Create
* ****************************************************************************
* </pre>
* @author Simba.Hua
*/
public class Printer {
private int counter;
public void print(String type) {
System.out.println(type + " printer " + counter ++);
}
}
package com.shux.springsource.lookup;
/**
*
* <pre>HISTORY
* ****************************************************************************
* ID DATE PERSON REASON
* 1 2018年1月18日 Simba.Hua Create
* ****************************************************************************
* </pre>
* @author Simba.Hua
*/
public abstract class HelloImpl implements HelloApi{
private Printer printer;
@Override
public void sayHello() {
printer.print("setter");
createPrototypePrinter().print("prototype");
createSingletonPrinter().print("singleton");
}
public abstract Printer createPrototypePrinter();
public Printer createSingletonPrinter() {
System.out.println("该方法不会被执行,如果执行了就出错了");
return new Printer();
}
public void setPrinter(Printer printer) {
this.printer = printer;
}
}
<!-- 测试look-up -->
<bean id="prototypePrinter" class="com.shux.springsource.lookup.Printer" scope="prototype"></bean>
<bean id="singletonPrinter" class="com.shux.springsource.lookup.Printer" scope="singleton"></bean>
<bean id="prototypeHelloApi" class="com.shux.springsource.lookup.HelloImpl" scope="prototype">
<property name="printer" ref="prototypePrinter"></property>
<lookup-method name="createPrototypePrinter" bean="prototypePrinter"/>
<lookup-method name="createSingletonPrinter" bean="singletonPrinter"/>
</bean>
<bean id="singletonHelloApi" class="com.shux.springsource.lookup.HelloImpl" scope="singleton">
<property name="printer" ref="prototypePrinter"></property>
<lookup-method name="createPrototypePrinter" bean="prototypePrinter"/>
<lookup-method name="createSingletonPrinter" bean="singletonPrinter"/>
</bean>
@Test
public void testLookUp() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mybeans.xml");
System.out.println("------------Singleton sayHello-----------");
HelloApi singletonHelloApi = context.getBean("singletonHelloApi", HelloApi.class);
singletonHelloApi.sayHello();
singletonHelloApi = context.getBean("singletonHelloApi", HelloApi.class);
singletonHelloApi.sayHello();
System.out.println("------------Prototype sayHello-----------");
HelloApi prototypeHelloApi = context.getBean("prototypeHelloApi", HelloApi.class);
prototypeHelloApi.sayHello();
prototypeHelloApi = context.getBean("prototypeHelloApi", HelloApi.class);
prototypeHelloApi.sayHello();
}
打印结果如下:
------------Singleton sayHello-----------
setter printer 0
prototype printer 0
singleton printer 0
setter printer 1
prototype printer 0
singleton printer 1
------------Prototype sayHello-----------
setter printer 0
prototype printer 0
singleton printer 2
setter printer 0
prototype printer 0
singleton printer 3