wowlalala 2019-11-19 20:07 采纳率: 55.6%
浏览 399
已采纳

java Spring scope设置为prototype,为什么两次生成的都是同一个实例

CurrentTime.java

import java.util.Calendar;

public class CurrentTime {

    private Calendar now=Calendar.getInstance();

    public void printCurrentTime() {
        System.out.println(now.getTime());
    }
}


lookup.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<bean id="currentTime" class="cn.hxex.springcore.lookup.CurrentTime" scope="prototype"></bean>
<bean id="lookupBean" class="cn.hxex.springcore.lookup.LookupBean"   scope="singleton">
<lookup-method name="createCurrentTime" bean="currentTime"></lookup-method>
<property name="currentTime"  ref="currentTime"></property>
</bean>                         
</beans>    

LookupMain.java

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class LookupMain {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
           ClassPathResource resource=new ClassPathResource("cn/hxex/springcore/lookup/LookupBeans.xml");
           XmlBeanFactory factory=new XmlBeanFactory(resource);
//           LookupBean lookupBean=(LookupBean) factory.getBean("lookupBean");
//           System.out.println("get");
//           lookupBean.getCurrentTime().printCurrentTime();
//           System.out.println("create");
//           lookupBean.createCurrentTime().printCurrentTime();
//           System.out.println("------------");
//           Thread.sleep(1000);
//           System.out.println("get");
//           lookupBean.getCurrentTime().printCurrentTime();
//           System.out.println("create");
//           lookupBean.createCurrentTime().printCurrentTime();
//           System.out.println("-----------");
           CurrentTime ct=(CurrentTime) factory.getBean("currentTime");
           System.out.println("一");
           ct.printCurrentTime();
           System.out.println("-----------");
           Thread.sleep(5000);
           System.out.println("二");
           ct.printCurrentTime();

    }

}

两次生成的实例是一样的

  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2019-11-20 09:39
    关注

    你这个测试方法并不能说明问题的呀,你打印了两次 ct ,同一个对象的时间属性当然是一样的啦。
    应该是间隔一段时间先后获取两次 Bean ID 相同的实例,再打印时间,肯定是不同的。这样测试试一下:

               CurrentTime ct1=(CurrentTime) factory.getBean("currentTime");
               System.out.println("一");
               ct1.printCurrentTime();
                         Thread.sleep(5000);
               CurrentTime ct2=(CurrentTime) factory.getBean("currentTime");
               System.out.println("一");
               ct2.printCurrentTime();
    
    

    prototype 方式,第二次获取时会创建一个新实例,时间也是不同的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题