开发者

Spring and the scope attribute

开发者 https://www.devze.com 2022-12-12 07:52 出处:网络
I am facing a problem in my Spring learning and would need some help. I was learning about the prototype scope of a bean, which basically mean that each time this bean will be required by someone or

I am facing a problem in my Spring learning and would need some help.

I was learning about the prototype scope of a bean, which basically mean that each time this bean will be required by someone or some other beans, Spring will create a new bean and not use the same one.

So I tried this bit of code, let's say 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 v开发者_如何学Pythonoid 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;
    } 
}

Nothing special here, some Strings, an Int and the getters 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 tried to play and see if my understanding of the prototype scope was right, with 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());            
    }
}

Surprisingly for me 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

So it seems that when I have updated the value of the product1 object, it has been updated as well for product 2. It seems to me to be a strange behaviour, isn't it ?


Your understanding of the prototype scope is right. From the documentation:

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).

That said, I couldn't reproduce the behavior you observed (I'm running the code you provided). This is what I get with 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

I didn't try all versions of Spring but you are likely using a buggy version (very unlikely though) or there is another problem somewhere (more likely).


prototype mode of a bean deployment results in the creation of a new bean instance every time a request for that specific bean is done.

So you are correct, each call should give a new instance.


Unable to reproduce:

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

Are you use you and it are looking at the same XML file?


Well I only have this context-file. But I think this is a problem with IntelliJ. Glad I understood this concept of prototype correctly which make my changes in this context-files totally ignored. I saw that when I had a bean in it and Spring said that there was no bean of this name when trying to instanciate it... Strange !

Thanks guys !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号