spring common notes - Scope

@The role of Scope

package common.config;

import common.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MainConfig2 {

    /*
     * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
     * @see ConfigurableBeanFactory#SCOPE_SINGLETON
     * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
     * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
     * prototype:Multi instance
     * singleton:Single instance
     * request:Create an instance at the same request
     * session:Create an instance of the same session
     */
    @Scope(value = "singleton")
    @Bean
    public Person person() {
        System.out.println("Establish person");
        return new Person("lisi", 21);
    }
}
package common;

import common.bean.Person;
import common.config.MainConfig2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainTest {
    @Test
    public void test() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
        System.out.println("IOC Container creation complete");
        Person person = (Person) applicationContext.getBean("person");
        Person person1 = (Person) applicationContext.getBean("person");
        System.out.println(person == person1);
    }
}

The print content of running test code is:

It can be seen that when the value of scope is singleton (the default value), when the IOC container starts, it will call methods to create objects and put them into the IOC container. In the future, each acquisition is directly obtained from the container. Therefore, in the case of a single instance, whenever an instance is obtained, it is the same.

Change to multiple instances again

package common.config;

import common.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MainConfig2 {

    /*
     * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
     * @see ConfigurableBeanFactory#SCOPE_SINGLETON
     * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
     * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
     * prototype:Multi instance
     * singleton:Single instance
     * request:Create an instance at the same request
     * session:Create an instance of the same session
     */
    @Scope(value = "prototype")
    @Bean
    public Person person() {
        System.out.println("Establish person");
        return new Person("lisi", 21);
    }
}

Execute the above test code, and the operation result is:

When the value of scope is prototype, the IOC container will not call methods to create objects in the container when it starts. Instead, the method is called to create the object each time it is acquired.

Tags: Session Junit

Posted on Fri, 10 Jan 2020 11:37:38 -0500 by VFRoland