springboot controller每次请求获取都是不同对象吗
在默认情况下,Spring Boot 控制器(Controller)每次请求获取的是不同的对象。这是因为 Spring Boot 默认会使用单例模式(singleton)来管理 Controller,这意味着所有请求都会共享同一个 Controller 对象。
然而,如果你想要每次请求都获取一个新的 Controller 对象,可以在 Controller 类上使用注解 @Scope("prototype"),这将告诉 Spring Boot 使用原型模式(prototype)来管理 Controller,从而每次请求都会创建一个新的 Controller 对象。需要注意的是,使用原型模式可能会带来一些性能开销,因为每次请求都需要创建一个新的对象。
示例代码:
@Controller
@Scope("prototype")
public class MyController {
// Controller code
}
原文地址: https://www.cveoy.top/t/topic/hwLA 著作权归作者所有。请勿转载和采集!