Spring and scope attribute

I ran into a problem in my Spring training and need some help.

I learned about a prototype of a bean scope, which basically means that every time this bean is required by someone or 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 class 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;
    } 
}

Nothing special here, some strings, int and 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 reproduce and see if I understood the prototype area correctly, 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 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:

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

So it seems that when I updated the value of the product1 object, it was also updated for product 2. It seems to me that this is strange behavior, right?

+3
4

. :

, bean bean , bean (.. bean getBean() ).

, , ( ). , spring-2.5.6.SEC01.jar:

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

Spring, , , - ( ) - ().

+6

bean bean , bean.

, , .

+1

:

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

XML?

0

, . , IntelliJ. , , . , bean, Spring , bean ... Strange!

, !

0

Source: https://habr.com/ru/post/1723229/


All Articles