1.prototype 每次对bean的请求生成一个新的的类.
2.singleton 每次获取的是同一个类,默认是这个
<bean id="testScopeDaoImpl" class="org.ymm.dao.impl.TestScopeDaoImpl" scope="prototype"></bean>
2.singleton 每次获取的是同一个类,默认是这个
<bean id="testScopeDaoImpl" class="org.ymm.dao.impl.TestScopeDaoImpl" scope="singleton"></bean>
BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml");
ITestScopeDao ts1 = bf.getBean("testScopeDaoImpl",ITestScopeDao.class);
ITestScopeDao ts2 = bf.getBean("testScopeDaoImpl",ITestScopeDao.class);
System.out.println(ts1.hashCode()+"----"+ts2.hashCode());
其他使用不多,不甚解
本文深入探讨了Spring框架中两种不同的Bean作用域:prototype和singleton。通过实例演示,阐述了它们在创建Bean实例时的不同行为,以及如何在实际应用中灵活运用这些特性。
974

被折叠的 条评论
为什么被折叠?



