首页 | 联系我们 | 叶凡网络官方QQ群:323842844
游客,欢迎您! 请登录 免费注册 忘记密码
您所在的位置:首页 > 开发语言 > Java开发 > 正文

lookup注入

作者:cocomyyz 来源: 日期:2013-08-04 23:28:35 人气:23 加入收藏 评论:0 标签:java

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>


本文网址:http://www.mingyangnet.com/html/java/154.html
读完这篇文章后,您心情如何?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
更多>>网友评论
发表评论
编辑推荐
  • 没有资料