Java – spring and scope properties

I have a problem learning spring and need some help

I'm learning the scope of the bean prototype, which basically means that every time someone or other beans need this bean, spring will create a new bean instead of using the same bean

So I tried this code. Suppose I have this product class:

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;
    } 
}

There is nothing special here. Some strings, an int and getter and setters Then I created this context file:

<?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>

Then I try to play and see if I understand the scope of the prototype correctly. This class:

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());            
    }
}

To my surprise, the answer is:

70.0
Let's change the price of this incredible game : 
Price for product1 object
80.0
Price Product 2 : 
80.0

Therefore, when I update the value of the product1 object, it seems that it has also updated product 2 In my opinion, this is a strange behavior, isn't it?

Solution

Your understanding of the scope of the prototype is correct From document:

That is, I can't reproduce the behavior you observed (I'm running the code you provided) This is what I use spring-2.5 6.SEC01. Jar obtained:

70.0
Let's change the price of this incredible game : 
Price for product1 object
80.0
Price Product 2 : 
70.0

I haven't tried all versions of spring, but you may use a defective Version (although very unlikely), or there is another problem somewhere (more likely)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>