[spring] What are the differences between Spring Bean scopes?

Preface

Spring's beans have 5 scopes: singleton, prototype, request, session and globalSession

What is the difference between Spring Bean scopes?

​ In Spring, you can set the scope of the bean in the scope attribute of the <bean> element to determine whether the bean is a single instance or multiple instances.

By default, Spring only creates a unique instance for each bean declared in the IOC container, and this instance can be shared throughout the IOC container: all subsequent getBean() calls and bean references will return this unique bean. Example. This scope is called a singleton, and it is the bean's default scope.

Scope category and description

  • singleton: There is only one Bean instance in the SpringIOC container, and the Bean exists as a single instance.
  • prototype: A new instance will be returned each time getBean() is called
  • request: A new Bean will be created for each HTTP request. This scope only applies to the WebApplicationContext environment.
  • session: The same HTTP Session shares a Bean, and different HTTP Sessions use different Beans. This scope only applies to the WebApplicationContext environment.

Brief description

Bean scope: You can specify the bean scope through the scope attribute of the element.

  • singleton: default value. When the IOC container is created, an instance of the bean will be created, and it is a singleton. You will get the same one every time.
  • prototype: prototype. When the IOC container is created, the bean is no longer instantiated. The bean is instantiated every time the getBean method is called, and every time the getBean method is called,
  • request: instantiate a bean for each request
  • session: share a bean within a session

test case

Because I usually use SPRING MVC for development, it is necessary to understand how to call these scopes.

1. Define java classes with different scopes

Java code

@Component  
@Scope( "session")  
public class SessionObj {
    
      
  
}  
@Component  
@Scope( "request")  
public class RequestObj {
    
      
  
}  
@Component  
@Scope( "prototype")  
public class PrototypeObj {
    
      
  
}  
@Component  
@Scope( "singleton")  
public class SingletonObj {
    
      
  
}  

2. Inject into the controller. Since the controller is a singleton, the object must be taken directly from the container by implementing the ApplicationContextAware interface.

So the controller under test is:

Java code

@Controller  
public class IndexController implements ApplicationContextAware {
    
      
  
    private RequestObj RequestObj;  
  
    private SessionObj SessionObj;  
  
    private PrototypeObj PrototypeObj;  
  
    private SingletonObj SingletonObj;  
  
    private ApplicationContext applicationContext;  
  
    @RequestMapping("/")  
    @ResponseBody  
    public String index() {
    
      
        print();  
        return "Welcome";  
    }  
  
    public void print() {
    
      
        System.out.println("first  time singleton is :" + getSingletonObj());  
        System.out.println("second time singleton is :" + getSingletonObj());  
  
        System.out.println("first  time prototype is :" + getPrototypeObj());  
        System.out.println("second time prototype is :" + getPrototypeObj());  
  
        System.out.println("first  time request is :" + getRequestObj());  
        System.out.println("second time request is :" + getRequestObj());  
  
        System.out.println("first  time session is :" + getSessionObj());  
        System.out.println("second time session is :" + getSessionObj());  
    }  
  
    @Override  
    public void setApplicationContext(ApplicationContext applicationContext)  
            throws BeansException {
    
      
        this.applicationContext = applicationContext;  
    }  
  
    public RequestObj getRequestObj() {
    
      
        return applicationContext.getBean(RequestObj.class);  
    }  
  
    public void setRequestObj(RequestObj requestObj) {
    
      
        RequestObj = requestObj;  
    }  
  
    public SessionObj getSessionObj() {
    
      
        return applicationContext.getBean(SessionObj.class);  
    }  
  
    public void setSessionObj(SessionObj sessionObj) {
    
      
        SessionObj = sessionObj;  
    }  
  
    public PrototypeObj getPrototypeObj() {
    
      
        return applicationContext.getBean(PrototypeObj.class);  
    }  
  
    public void setPrototypeObj(PrototypeObj prototypeObj) {
    
      
        PrototypeObj = prototypeObj;  
    }  
  
    public SingletonObj getSingletonObj() {
    
      
        return applicationContext.getBean(SingletonObj.class);  
    }  
  
    public void setSingletonObj(SingletonObj singletonObj) {
    
      
        SingletonObj = singletonObj;  
    }  
  
}  
 
 

3. Running results

Java code

//使用chrome第一次打印数据:  
first  time singleton is :com.fb.po.SingletonObj@1e3223e  
second time singleton is :com.fb.po.SingletonObj@1e3223e  
first  time prototype is :com.fb.po.PrototypeObj@3e683f  
second time prototype is :com.fb.po.PrototypeObj@12e18d7  
first  time request is :com.fb.po.RequestObj@1d45706  
second time request is :com.fb.po.RequestObj@1d45706  
first  time session is :com.fb.po.SessionObj@9a6b2e  
second time session is :com.fb.po.SessionObj@9a6b2e  
  
  
  
//使用chrome打印第二次数据  
first  time singleton is :com.fb.po.SingletonObj@1e3223e  
second time singleton is :com.fb.po.SingletonObj@1e3223e  
first  time prototype is :com.fb.po.PrototypeObj@122e5be  
second time prototype is :com.fb.po.PrototypeObj@192add  
first  time request is :com.fb.po.RequestObj@4d1b6c  
second time request is :com.fb.po.RequestObj@4d1b6c  
first  time session is :com.fb.po.SessionObj@9a6b2e  
second time session is :com.fb.po.SessionObj@9a6b2e  
  
  
  
//使用IE打印第三次数据  
first  time singleton is :com.fb.po.SingletonObj@1e3223e  
second time singleton is :com.fb.po.SingletonObj@1e3223e  
first  time prototype is :com.fb.po.PrototypeObj@10f1ecb  
second time prototype is :com.fb.po.PrototypeObj@1aeb990  
first  time request is :com.fb.po.RequestObj@18a1e7  
second time request is :com.fb.po.RequestObj@18a1e7  
first  time session is :com.fb.po.SessionObj@12d5c55  
second time session is :com.fb.po.SessionObj@12d5c55  
 

4. Result analysis

Judging from the results, the data of the singleton bean is printed the same three times (the default bean level is singleton); the
data of the prototype bean is different each time, and it is called twice for each request. The results are all different.
The request bean is inconsistent every time the request is made, but the data returned by the same request is consistent.
The results of the session bean are consistent in the first two times, but the data is inconsistent in the last time, which is consistent with the rhythm of the session.

5. Lack

It is said on the Internet that
Xml code must be configured

<listener>   
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>   
</listener>   

But it works fine without configuration... I'm curious...

The last scope is suitable for portlets. I have not tested it. It is said that it can be shared between multiple sessions, and the effect is equivalent to global variables.

Guess you like

Origin blog.csdn.net/u011397981/article/details/132846812