首先看个小demo

@RestController
public class TestController {

    private int i = 0;
    @GetMapping(value = "/test1")
    public int testInstance1(){
        i++;
        return i;
    }
    @GetMapping(value = "/test2")
    public int testInstance2(){
        i++;
        return i;
    }
}

依次访问

http://localhost:8082/reed/test1 //返回1

http://localhost:8082/reed/test1 //返回2

http://localhost:8082/reed/test2 //返回3

可以看出。所以请求共享一个对象。

-----------------------------------------------------------------------------------

若在方法前面加上@Scope("prototype") 指定为享元模式

@RestController
@Scope("prototype")
public class HelloController

依次访问

http://localhost:8082/reed/test1 //返回1

http://localhost:8082/reed/test1 //返回1

http://localhost:8082/reed/test2 //返回1

可以看出变为多例,每次请求新建一个对象

-----

为什么默认采用单例呢?

1.多例每次都要去新建对象,单例只需要实例化一个对象。因此,单例会快很多。

2.如果Controller中没有属性,完全不需要用多例。