Easy to understand spring singleton and prototype

Keywords: Java xml Spring encoding

As for spring bean scopes, based on different containers, there will be differences, such as BeanFactory and Application Context containers. In this article, we mainly discuss the bean scopes based on Application Context containers.

As for the scope of bean s, spring mainly includes singleton,prototype,session,request,global. This article mainly describes two commonly used types: singleton and prototype.

I. singleton

Singleton is a singleton pattern, that is, a bean with scope="singleton", which is instantiated only once in the container.

dao sample code:

package com.demo.dao;

public class UserDao {

    public UserDao(){
        System.out.println("UserDao The parametric constructor is called");
    }
    //Get the username
    public String getUserName(){
        //Simulated dao layer
        return "Alan_beijing";
    }
}

applicationContext.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">

    <bean class="com.demo.dao.UserDao" id="userDao" scope="singleton"/>
</beans>

test:

public class MyTest {

    @Test
    public void test(){
        //Define containers and initialize them
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //Define the first object
        UserDao userDao = applicationContext.getBean(UserDao.class);
        System.out.println(userDao.getUserName());

        //Define the second object
        UserDao userDao2 = (UserDao) applicationContext.getBean("userDao");
        System.out.println(userDao2.getUserName());
        //Compare whether two object instances are the same
        System.out.println("The first example:"+userDao+"\n"+"The second example:"+userDao2);
    }
}

Test results:

Analysis: In the test code, beans are defined as singletons, and the getBean() method of ApplicationContext is used twice to get beans (userDao), but the same instance object is returned: com.demo.dao.UserDao@27a5f880. Observed carefully, although the beans are obtained twice, the userDao's parametric constructor is only called once, which proves that singletons are actually only called once in containers. Once instantiated, it is important to note that the beans in Singleton mode, when the Application Context loads the beans, instantiate the beans.

Define bean s:

Test results:

The following code simply loads the beans, but does not call the getBean method to get the beans, but UserDao is called once, that is, instantiate.

Two prototype s

Prototype is the prototype pattern, which can be instantiated as many times as a bean is called.

Change singleton code to prototype

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

    <bean class="com.demo.dao.UserDao" id="userDao" scope="prototype"/>
</beans>

The test code is the same as singleton, but the results are different:

Analysis: Through the test results, it is not difficult to find that the UserDao object is instantiated twice by calling two beans, and the objects are different. It should be noted that beans of prototype type type type are instantiated only when beans are acquired.

The difference between three singleton s and prototype

(1)singleton is instantiated only once in the container, while prototype is instantiated several times in the container when it is called several times.

(2) In the AppplicationContext container, singleton is pre-instantiated when the applicaitonContext.xml is loaded, while prototype must be instantiated only when it is called

   singleton:

Define bean s:

Test:

 prototype:

Define bean:

Test: No calls

Test: Call

4.singleton consumes more performance than prototype. In web development, singleton mode is recommended. In app development, prototype mode is recommended.

Fourth Copyright Zone

To reprint a blog, the source of the blog must be indicated.
Blog Garden: http://www.cnblogs.com/wangjiming/(with emphasis on.NET)
CSDN: https://blog.csdn.net/u010228798 (focusing on JAVA)
If you have any new ideas, please send them to 2098469527@qq.com.
Professional. NET Home Technology QQ Group: 490539956
Specialized Java House QQ Group: 924412846
QQ group: 2098469527
One-to-one technical guidance QQ: 2098469527

Posted by robogenus on Fri, 11 Oct 2019 05:48:32 -0700