Scope of spring learning bean

Keywords: Java xml Spring Session jvm

scope

It includes Singleton, Prototype, request, session, application and websocket. It mainly talks about the commonly used Singleton and Prototype.

Singleton

When a bean is defined as a singleton object, the IoC container creates only one instance of the bean object. The bean object is stored in the container's cache (map). Subsequent references to the bean are taken from the cache (map.get).
This singleton is different from the singleton pattern of the design pattern. spring singleton means that there is only one instance in the container, while design pattern singleton means that there is only one instance in the JVM process.

Prototype

When you need to getBean every time, you need to return different instances. At this time, you need to use Prototype.

Singleton and Prototype

If the bean is stateless, you need to use Singleton. If it is stateful, you need to use Prototype.
For Singleton's bean s, the spring container manages its entire life cycle.
For Prototype bean s, the spring container does not manage its entire life cycle. Although the initialized method will be called, it has nothing to do with the scope. The destroy method is not called when the container is closed.

Example

XML

XML configuration:

<?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 id="singletonBean" class="com.learn.beanScope.SingletonBean" scope="singleton"/>
    <bean id="prototypeBean" class="com.learn.beanScope.PrototypeBean" scope="prototype"/>

</beans>

Test code

@Test
public void test() {
    ApplicationContext app = new ClassPathXmlApplicationContext("beanScope.xml");
    SingletonBean singletonBean1 = app.getBean("singletonBean",SingletonBean.class);
    SingletonBean singletonBean2 = app.getBean("singletonBean",SingletonBean.class);
    PrototypeBean prototypeBean1 =app.getBean("prototypeBean",PrototypeBean.class);
    PrototypeBean prototypeBean2 =app.getBean("prototypeBean",PrototypeBean.class);
    System.out.println(singletonBean1);
    System.out.println(singletonBean2);
    System.out.println(prototypeBean1);
    System.out.println(prototypeBean2);
}

Operation result:

When the scope is Singleton, you can see that the address is the same from the container twice, so it is the same bean.
When the scope is Prototype, you can see that the address is different from the container twice, so it is not the same bean.

annotation

MyConfig

@Configuration
public class MyConfig {
    @Bean
    @Scope("prototype")
    public PrototypeBean prototypeBean() {
        return new PrototypeBean();
    }

    @Bean
    @Scope("singleton")
    public SingletonBean singletonBean() {
        return new SingletonBean();
    }
}

Test code

@Test
public void test() {
    ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class);
    SingletonBean singletonBean1 = app.getBean("singletonBean",SingletonBean.class);
    SingletonBean singletonBean2 = app.getBean("singletonBean",SingletonBean.class);
    PrototypeBean prototypeBean1 =app.getBean("prototypeBean",PrototypeBean.class);
    PrototypeBean prototypeBean2 =app.getBean("prototypeBean",PrototypeBean.class);
    System.out.println(singletonBean1);
    System.out.println(singletonBean2);
    System.out.println(prototypeBean1);
    System.out.println(prototypeBean2);
}

Operation result:

The result is the same as XML.

Posted by dillonit on Tue, 15 Oct 2019 19:08:41 -0700