bean 的作用域singleton和prototype的区别
程序员文章站
2022-05-24 10:53:54
...
先看代码:
当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]
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]