Skip to content

一、四种作用域的实现

  • singleton:全局有且仅有一个实例 (节约内存)。
  • prototype:每次获取Bean 的时候都会有一个新的实例 (消耗内存)。
  • request:表示针对每次请求都会产生一个新的 Bean 对象,并且该 Bean 仅在当前 Http 请求内有效。
  • session: 作用域表示请求都会产生一个新的 Bean 对象, 并且该 Bean 仅在当前 Http session 内有效。

默认情况下,Spring 容器是注入对象的作用域为singleton (单例), 为了节省内存资源开销。

1. singleton 单例模式

  • 在 @Controller 下添加:@Scope(value="singleton"), 默认不添加为单例。
java
package org.example.spring5.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 14:17 <br>
 * @since: 1.0 <br>
 * @description: UserController <br>
 */
@Controller
@Scope
public class UserController {

}
  • 判断从Spring 容器获取对象是否是同一个引用地址对象。
java
package org.example.spring5;

import org.example.spring5.config.SpringConfiguration;
import org.example.spring5.controller.UserController;
import org.example.spring5.dao.UserDao;
import org.example.spring5.domain.User;
import org.example.spring5.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 14:27 <br>
 * @since: 1.0 <br>
 * @description: SpringApplication <br>
 */
public class SpringApplication {

    private static AnnotationConfigApplicationContext annotationConfigApplicationContext;

    public static void main(String[] args) {
        annotationConfigApplicationContext  = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        // @Controller 的方式创建和调用对象
        UserController userControllerBean = annotationConfigApplicationContext.getBean(UserController.class);
        System.out.println(userControllerBean);

        UserController userControllerBean1 = annotationConfigApplicationContext.getBean(UserController.class);
        System.out.println(userControllerBean1);

        System.out.println("userControllerBean 和 userControllerBean1 对象是引用调用同一个对象" + userControllerBean.equals(userControllerBean1));
    }
}
  • 输出结果
log
# 输出结果
org.example.spring5.controller.UserController@4efc180e
org.example.spring5.controller.UserController@4efc180e
userControllerBean 和 userControllerBean1 对象是引用调用同一个对象true

2. prototype 原型(多例)模式

  • 在 @Controller 下添加:@Scope(value="prototype")
java
package org.example.spring5.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 14:17 <br>
 * @since: 1.0 <br>
 * @description: UserController <br>
 */
@Controller
@Scope(value = "prototype")
public class UserController {

}
  • 判断从Spring 容器获取对象是否是同一个引用地址对象。
java
package org.example.spring5;

import org.example.spring5.config.SpringConfiguration;
import org.example.spring5.controller.UserController;
import org.example.spring5.dao.UserDao;
import org.example.spring5.domain.User;
import org.example.spring5.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 14:27 <br>
 * @since: 1.0 <br>
 * @description: SpringApplication <br>
 */
public class SpringApplication {

    private static AnnotationConfigApplicationContext annotationConfigApplicationContext;

    public static void main(String[] args) {
        annotationConfigApplicationContext  = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        // @Controller 的方式创建和调用对象
        UserController userControllerBean = annotationConfigApplicationContext.getBean(UserController.class);
        System.out.println(userControllerBean);

        UserController userControllerBean1 = annotationConfigApplicationContext.getBean(UserController.class);
        System.out.println(userControllerBean1);

        System.out.println("userControllerBean 和 userControllerBean1 对象是引用调用同一个对象" + userControllerBean.equals(userControllerBean1));
    }
}
  • 输出结果
log
# 输出结果
org.example.spring5.controller.UserController@4efc180e
org.example.spring5.controller.UserController@bd4dc25
userControllerBean 和 userControllerBean1 对象是引用调用同一个对象false

3. request 请求模式

4. session session模式