Explore the six scopes of Spring Bean: understand the applicable scenarios and usage methods

Mainly explain the singleton scope and prototype scope, and understand the remaining four

Singleton scope:

Singleton scope is generally the default bean scope. The Spring container creates an instance the first time it gets a bean, and returns the same instance on subsequent requests.

For example:
we now create a public bean for user one and user two to use. After the user repeatedly uses it, modify its content, and then it is used by user two. Check whether the bean has changed, and you can check the scope of the bean.

Public Bean:

@Component
public class Users {
    
    
   private User user;
    @Bean
    public User user1(){
    
    
       user= new User(1);
       user.setName("zcx");
       return user;
    }
}

Modify operation when the user uses it:

@Controller
public class BeanScopesController {
    
    
    @Autowired
    private User user;
    public  User getUser(){
    
    
        User user1 = user;
        System.out.println(user1.toString()+"这是修改之前的");
        user1.setName("java");
        return  user1;
    }
}

User two goes to use the bean again:

@Controller
public class BeanScopesController2 {
    
    
    @Autowired
    private User user2;
    public  User getUser2(){
    
    
        User user = user2;
        return user;
    }
}

Print the value of the Bean shared by user 1 and user 2:

public class demo {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            BeanScopesController beanScopesController = context.getBean(BeanScopesController.class);
        System.out.println(beanScopesController.getUser().toString()+"这是第一个调用对象修改之后的");
        BeanScopesController2 beanScopesController2 = context.getBean(BeanScopesController2.class);
        System.out.println(beanScopesController2.getUser2().toString()+"这是第二个对象调用之后的");

    }
}

Print the result:
insert image description here
Through the results, we found that the value modified by user 1 also appeared in user 2, which is no longer the initial value.
So it shows that Bean is singleton by default, that is, all users use the same object.

Prototype scope:

Each request for a bean under the scope will create a new instance: obtaining a bean (that is, obtaining it through applicationContext.getBean, etc.) and assembling a bean (that is, injecting it through @Autowired) are new object instances

You can use the @Scope tag to modify the scope of a bean, for example:

@Component
public class Users {
    
    
   private User user;
   @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean
    public User user1(){
    
    
       user= new User(1);
       user.setName("zcx");
       return user;
    }
}

The result of printing the same code as before is as follows: From
insert image description here
the result, we know that the scope of the Bean has been changed from the singleton mode to the prototype mode, that is, the multi-instance mode.
Of course, there are two ways to set the @Scope tag:

  1. Set the value directly: @Scope("prototype")
  2. Use enumeration settings: @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Request (Request) scope:

Each http request will create a new bean instance, similar to prototype, an http request and response shared bean, limited to use in SpringMVC

Session scope:

In an http session, define a Bean instance, the shared Bean of the user session, limited to use in SpringMVC

Global (application) scope:

In an http servlet Context, define a Bean instance, which is used for the context information of the web application, and is limited to use in SpringMVC

HTTP WebSocket scope

In the life cycle of an HTTP WebSocket, a Bean instance is defined. In each WebSocket session, a Map structure header information is saved, which will be used to wrap the client message header. After the first initialization, it is the same bean until the end of WebSocket, which is limited to use in Spring WebSocket

Guess you like

Origin blog.csdn.net/st200112266/article/details/131949276