bean 的作用域singleton和prototype的区别

先看代码:

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

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

你可能感兴趣的:(xml,bean,prototype,Blog)