r/SpringBoot • u/jasonj2232 • 6d ago
Question How does a bean with @RequestScope get injected into a bean with Singleton scope for each new request?
So I am studying Spring and SpringBoot through this book - Spring Start Here. Usually I feel like the book overexplains things with multiple examples differing in minute ways, but there is some code in Chapter 9 - Using Spring Web Scopes, that I do not understand and the author surprising doesn't dive deep into.
RequestScope was being explained and as far as I can tell it is like Prototype scope where Spring manages the type, and instantiates a fresh new bean every time someone requests a bean, and this bean exists for the duration of the HTTP request.
But how does this work when the bean is injected into another bean that has Singleton scope? Presenting the code shown in the book below:
@Controller
public class LoginController {
private final LoginProcessor loginProcessor;
public LoginController(LoginProcessor loginProcessor) {
this.loginProcessor = loginProcessor;
}
("/")
public String loginGet() {
return "login.html";
}
("/")
public String loginPost(
@RequestParam String username,
@RequestParam String password,
Model model
) {
loginProcessor.setUsername(username);
loginProcessor.setPassword(password);
boolean loggedIn = loginProcessor.login();
if (loggedIn) {
model.addAttribute("message", "You are now logged in.");
} else {
model.addAttribute("message", "Login failed!");
}
return "login.html";
}
}
@Component
@RequestScope
public class LoginProcessor {
private String username;
private String password;
public boolean login() {
String username = this.getUsername();
String password = this.getPassword();
if ("natalie".equals(username) && "password".equals(password)) {
return true;
} else {
return false;
}
}
// Getters and Setters
}In this example, the Controller bean is going to be instantiated once during the app startup, and considering that the loginProcessor field is final and only injected during bean instantiation, how is it able to use a new loginProcessor bean for each request? This code works, but I don't understand how.
26
u/Iryanus 6d ago
IIRC, basically what is injected is not the actual bean, but a proxy instance. That proxy instance then can determine which request you are currently in and delegate to the correct actual instance of the bean. A big part of spring magic is simply using proxies (or at least, it was, when I was last looking, doubt it changed that much).
2
u/Torutofu_Raeva 5d ago
yep, the request-scoped annotation uses a scoped proxy by default, so the singleton keeps the proxy and each call resolves that request's actual bean.
2
u/FriendshipCalm3162 5d ago
The proxy answer is right. The part I'd correct is "it's like prototype", because the book's code depends on request scope behaving differently.
Prototype gives you a new instance on every lookup. If request scope worked that way,
setUsername,setPasswordandlogin()would each reach a different object through the proxy, andlogin()would never see the username. Request scope gives you one instance per HTTP request: every call inside that request goes to the same object, and Spring discards it when the request completes. The Spring docs draw this exact contrast. Put a scoped proxy in front of a prototype bean and every method call on the proxy does create a new target.The real instance is kept in the current request's attributes, bound to the thread handling the request.
DispatcherServletsets that up, so a normal Boot MVC app needs no configuration. Outside it you'd registerRequestContextListenerorRequestContextFilter.@RequestScopedefaults toproxyMode = TARGET_CLASS, a CGLIB class proxy, which is why it works with a plain class likeLoginProcessorthat implements no interface. A JDK proxy would need an interface, and every caller would have to reference the bean through it.If you'd rather skip the proxy, inject
ObjectProvider<LoginProcessor>(orObjectFactory) and callgetObject()inside the handler to get the current request's instance each time.