欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

bean 的作用域singleton和prototype的区别

程序员文章站 2022-05-24 10:53:54
...
先看代码:

PersonBean person=(PersonBean)context.getBean("personBean");   
System.out.println(person.getName());
person.setName("linshi");
System.out.println(person.getName());
PersonBean newPerson=(PersonBean)context.getBean("personBean");
System.out.println(newPerson.getName());

PersonBean person=(PersonBean)context.getBean("personBean");
System.out.println(person.getName());
person.setName("linshi");
System.out.println(person.getName());
PersonBean newPerson=(PersonBean)context.getBean("personBean");
System.out.println(newPerson.getName());

当XML文件的配置为:

<bean id="personBean" class="wang.PersonBean" >
<property name="name" value="${name}" />
<property name="id" value="${id}" />
</bean>

PersonBean的作用域为默认singleton时,输出结果是:

zhanshan
linshi
linshi

将XML文件的配置更改为:

<bean id="personBean" class="wang.PersonBean" scope="prototype">
<property name="name" value="${name}" />
<property name="id" value="${id}" />
</bean>

输出结果是:

zhanshan
linshi
zhanshan

转载自:[url]http://shaoxiongwang.iteye.com/blog/245058[/url]