Spring6 – (06) Bean scope

Article directory

  • Spring6 – (04) Bean scope
    • 1. singleton
    • 2. prototype
    • 3. Other scopes

Spring6 – (04) Bean scope

1. singleton

By default, the Bean object created by Spring’s IoC container is a singleton.

Step 1: Create a SpringBean class

package com.julissa.spring6.bean;

public class SpringBean {<!-- -->
    
}

The second step: configure beans.xml

<?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.xsd">

    <!-- Configure SprigBean -->
    <!-- Default: singleton -->
    <bean id="springBean" class="com.julissa.spring6.bean.SpringBean"/>
</beans>

Step 3: Programming Test Program

@Test
public void test(){<!-- -->
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println("The first SpringBean object:" + springBean);
    SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println("The second SpringBean object:" + springBean2);
}

Step 4: Run the test program

Through the test, we know: In Spring’s IoC container, by default, the Bean object is a singleton.

When was this object created? You can provide a parameterless construction method for SpringBean, test it

Step 1: Modify the SpringBean class

package com.julissa.spring6.bean;

public class SpringBean {<!-- -->
    public SpringBean() {<!-- -->
        System.out.println("The no-argument construction method of SpringBean is executed.");
    }
}

Step 2: Write a test program

@Test
public void test(){<!-- -->
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
}

Step 3: Run the test program

image-20230319140524392

According to the test, By default, the creation of the Bean object is completed when the Spring context is initialized.

Key points:

  • Bean is a singleton by default (singleton: singleton)
  • Instantiate when initializing the Spring context
  • Every time the getBean() method is called, the singleton object is returned

2. prototype

If you want Spring’s Bean object to exist in the form of multiple instances, you can specify the value of the scope attribute in the bean tag: prototype,

In this way, Spring will create a Bean object every time the getBean() method is executed, and it will be created several times when it is called several times.

Step 1: Create a SpringBean class

package com.julissa.spring6.bean;

public class SpringBean {<!-- -->
    public SpringBean() {<!-- -->
        System.out.println("The no-argument construction method of SpringBean is executed.");
    }
}

The second step: configure beans.xml

<?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.xsd">

    <!-- Configure SprigBean -->
    <!-- prototype -->
    <bean id="springBean" class="com.julissa.spring6.bean.SpringBean" scope="prototype"/>
</beans>

Step 3: Programming Test Program

@Test
public void test(){<!-- -->
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println("The first SpringBean object:" + springBean);
    SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println("The second SpringBean object:" + springBean2);
}

Step 4: Run the test program

image-20230319140904223

Comment out the line of code where the getBean() method is located in the test code:

@Test
public void test(){<!-- -->
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
}

Run the test program

image-20230319140958585

It can be seen that this time when the Spring context was initialized, the Bean object was not created.

Then you may ask: If the scope is not configured, what is its default value? The default value is singleton, a singleton.

Modify beans.xml

<?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.xsd">

    <!-- Configure SprigBean -->
    <bean id="springBean" class="com.julissa.spring6.bean.SpringBean" scope="singleton"/>
</beans>

Write a test program

@Test
public void test(){<!-- -->
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println("The first SpringBean object:" + springBean);
    SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println("The second SpringBean object:" + springBean2);
}

Run the test program

image-20230319141313976

According to the test, When the scope attribute is not specified, the default is the singleton singleton.

Key points:

  • When the scope attribute of the Bean is set to prototype, the Bean is multiple instances
  • When Spring initializes the context, it does not instantiate the bean object
  • Instantiate the Bean object every time the getBean() method is executed

3. Other scope

The value of the scope attribute is more than two, it includes 8 options in total:

  • singleton: default, singleton.
  • prototype: prototype. Each time the getBean() method is called, a new Bean object is obtained. Or a new object every time it is injected.
  • request: A request corresponds to a Bean. Limited to use in WEB applications.
  • session: A session corresponds to a Bean. Limited to use in WEB applications.
  • global session: dedicated in the portlet application. If the global session is used in the Servlet WEB application, it has the same effect as the session. (Portlets and servlets are both specifications. Servlets run in servlet containers, such as Tomcat. Portlets run in portlet containers.)
  • application: An application corresponds to a Bean. Limited to use in WEB applications.
  • websocket: A websocket life cycle corresponds to a bean. Limited to use in WEB applications.
  • Custom scope: Rarely used.