The scope of the Bean

In Spring, beans are created as singleton objects by default, although they can be modified based on actual requirements. We can think of this as setting the scope of the bean. This is implemented in the form of configuration files and annotations, but in either case there are four scopes.

The four scopes are:

Singleton (default) : Indicates that the object being created is singleton. Prototype: the created object is multi-instance. Request: Generates one instance at a time. Session: Generates one session instance.Copy the code

Note: Singleton and Prototype are more commonly used in these four scopes, and are also used in demonstrations.

Code demo

Write two Java classes for the demonstration

public class User {}public class Car {}Copy the code

Set the scope of the bean in XML form

1. Write a configuration file.

Set the scope of the bean by setting the scope property of the bean label.

<! Inject the User class and set the bean to singleton. Scope ="singleton" can be omitted because spring creates a single instance of the bean by default -->
<bean id="user" class="com.springtest.bean.User" scope="singleton"/>
<! Set the bean to multiple instances -->
<bean id="car" class="com.springtest.bean.Car" scope="prototype"/>
Copy the code

2. Write test classes for verification.

@Test
public void test1(a) {
    // Create the IOC container for Spring
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    // User is set to a single instance, Car is set to a multi-instance. Verify by comparing addresses.
    Object user1 = context.getBean("user");
    Object user2 = context.getBean("user");
    if(user1 == user2) {
      System.out.println("The User created through Spring is a singleton!");
    } else {
      System.out.println("Users created through Spring are multi-instance objects!");
    }
    Object car1 = context.getBean("car");
    Object car2 = context.getBean("car");
    if(car1 == car2) {
      System.out.println("Car created through Spring is a singleton!");
    } else {
      System.out.println("Cars created through Spring are multi-instance objects!"); }}Copy the code

3. Verify the results.

Set the scope of the bean through annotations

Use the @scope annotation to set the scope of the bean. This annotation can be applied to classes and methods. Set the scope of the bean by setting the value of the annotation value property. The value property has the same range of values as when set in XML.

Write configuration classes and annotate beans with @bean annotations.

@Configuration
public class BeanScopeConfig {

  @Bean
  @Scope(value = "prototype") // Set User to multiple instances
  public User user(a) {
    return new User();
  }

  @Bean
  @Scope(value = "singleton") // Set Car to a single instance
  public Car car(a) {
    return newCar(); }}Copy the code

2. Write test classes for verification.

@Test
public void test2(a) {
    // Create the IOC container for Spring
    ApplicationContext context = new AnnotationConfigApplicationContext(BeanScopeConfig.class);
    // User is set to multi-instance, Car is set to single-instance. Verify by comparing addresses.
    Object user1 = context.getBean("user");
    Object user2 = context.getBean("user");
    String userScope = user1 == user2 ? "User is a singleton!" : "User multi-instance object!";
    System.out.println(userScope);
    Object car1 = context.getBean("car");
    Object car2 = context.getBean("car");
    String carScope = car1 == car2 ? "Car is a singleton!" : "Car multi-instance object!";
    System.out.println(carScope);
}
Copy the code

3. Verify the results.

This is the end of today’s sharing, if you feel that the article written by “rookie” is still good, remember to like, forward and pay attention to yo! Your support is what keeps me going. Article where to write problems also hope that you can point out, I will be modestly taught.