LookupBean.java
package com.brj.test;
import java.util.Date;
public abstract class LookupBean {
private CurrentTime currentTime;
public CurrentTime getCurrentTime() {
return currentTime;
}
public void setCurrentTime(CurrentTime currentTime) {
this.currentTime = currentTime;
}
public void showCurrentTime(){
createCurrentTime().printCurrentTime();
}
public abstract CurrentTime createCurrentTime();
//=====================================================
private Date nowTime;
public Date getNowTime() {
return nowTime;
}
public void setNowTime(Date nowTime) {
this.nowTime = nowTime;
}
public void showNowTime(){
System.out.println(createNowTime());
}
public abstract Date createNowTime();
}
CurrentTime.java
package com.brj.test;
import java.util.Calendar;
public class CurrentTime {
private Calendar now = Calendar.getInstance();
public void printCurrentTime(){
System.out.println("CurrentTime: " + now.getTimeInMillis());
}
}
TestLookup.java
package com.brj.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class TestLookup {
public static void main(String agrs[]){
ClassPathResource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
LookupBean lookup = (LookupBean) factory.getBean("lookupBean");
lookup.showCurrentTime();
try {
Thread.sleep(1124);
} catch (InterruptedException e) {
e.printStackTrace();
}
lookup = (LookupBean) factory.getBean("lookupBean");
lookup.showCurrentTime();
}
}
applicationContext.xml
<bean
id="currentTime"
class="com.brj.test.CurrentTime"
scope="prototype">
</bean>
<bean
id="nowTime"
class="java.util.Date"
scope="prototype">
</bean>
<bean
id="lookupBean"
class="com.brj.test.LookupBean"
scope="singleton">
<lookup-method bean="currentTime" name="createCurrentTime"/>
<property name="currentTime" ref="currentTime"></property>
</bean>