1.applicationContext.xml:
<bean id="singleBean" class="util.SingleBean" scope="singleton">
<lookup-method name="create" bean="prototypeBean"/>
<property name="dynamicBean" ref="prototypeBean"></property>
</bean>
<bean id="prototypeBean" class="util.DynamicBean" scope="prototype" > </bean>
property和lookup-method的区别:
property: set的bean是一个单态的.虽然在它所创建的bean是个prototype类型的.
lookup-method: 抽象方法创建的bean是prototype类型的.
这就是一个单态Bean每次都会调用一个新的prototype Bean.
2.SingleBean:
public abstract class SingleBean {
private DynamicBean dynamicBean;
public DynamicBean getDynamicBean() {
return dynamicBean;}
public void setDynamicBean(DynamicBean dynamicBean) {
this.dynamicBean = dynamicBean;}
public void single(){
dynamicBean.print();}
public abstract DynamicBean create(); //和lookup-method对应
}
3.DynamicBean:
public class DynamicBean {
private Date date=new Date();
public void print(){
System.out.println(date);
}
}
-----------------------