`
feiliboos
  • 浏览: 665685 次
文章分类
社区版块
存档分类
最新评论

配置Spring管理的bean的作用域

 
阅读更多

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="singleton" ></bean>

这里的scope值有2种,singleton和prototype。singleton是单例模式,意思是每次实例化的bean不变都相同;而prototype

是多列模式,每次实例化后的bean都不相同。

在junit测试类中可以这样进行判断:

@Test
public void instanceSpring(){
AbstractXmlApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
PersonServiceBean ps=(PersonServiceBean) ctx.getBean("personService");
PersonServiceBean ps2=(PersonServiceBean) ctx.getBean("personService");
System.out.println(ps==ps2 );
}

如果返回值为true则说明scope=“singleton”,反之则scope="prototype"。默认时是singleton状态

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics