java – Spring和scope属性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Spring和scope属性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3110字,纯文字阅读大概需要5分钟。
内容图文
![java – Spring和scope属性](/upload/InfoBanner/zyjiaocheng/742/27e182e87fee440a9a16c6aa22d6dfdc.jpg)
我在Spring学习中遇到问题,需要一些帮助.
我正在学习bean的原型范围,这基本上意味着每次有人或其他bean需要这个bean时,Spring会创建一个新bean而不使用相同的bean.
所以我尝试了这段代码,假设我有这个Product类:
public class Product {
private String categoryOfProduct;
private String name;
private String brand;
private double price;
public String getCategoryOfProduct() {
return categoryOfProduct;
}
public void setCategoryOfProduct(String categoryOfProduct) {
this.categoryOfProduct = categoryOfProduct;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
这里没什么特别的,一些Strings,一个Int和getter and setters.
然后我创建了这个上下文文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="product" class="com.springDiscovery.org.product.Product" scope="prototype">
<property name="brand" value="Sega"/>
<property name="categoryOfProduct" value="Video Games"/>
<property name="name" value="Sonic the Hedgehog"/>
<property name="price" value="70"/>
</bean>
</beans>
然后我试着玩,看看我对原型范围的理解是否正确,这个类:
package com.springDiscovery.org.menu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springDiscovery.org.product.Product;
public class menu {
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
Product product1 = (Product) context.getBean("product");
Product product2 = (Product) context.getBean("product");
System.out.println(product1.getPrice());
System.out.println("Let's change the price of this incredible game : ");
product1.setPrice(80);
System.out.println("Price for product1 object");
System.out.println(product1.getPrice());
System.out.println("Price Product 2 : ");
System.out.println(product2.getPrice());
}
}
令我惊讶的是,答案是:
70.0
Let's change the price of this incredible game :
Price for product1 object
80.0
Price Product 2 :
80.0
因此,当我更新了product1对象的值时,它似乎也已经更新了产品2.在我看来这是一种奇怪的行为,不是吗?
解决方法:
您对原型范围的理解是正确的.从文档:
The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made (that is, it is injected into another bean or it is requested via a programmatic
getBean()
method call on the container).
也就是说,我无法重现您观察到的行为(我正在运行您提供的代码).这是我用spring-2.5.6.SEC01.jar得到的:
70.0 Let's change the price of this incredible game : Price for product1 object 80.0 Price Product 2 : 70.0
我没有尝试所有版本的Spring,但你可能会使用一个有缺陷的版本(虽然非常不可能),或者某个地方存在另一个问题(更有可能).
内容总结
以上是互联网集市为您收集整理的java – Spring和scope属性全部内容,希望文章能够帮你解决java – Spring和scope属性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。