There is a controller (see listing), it defines the loginprocesor field, which is initialized when creating the controller (using dependency injection). It is worth noting that instances of the LoginProccesor bean have a scope within the request. Question: why is it that every time the controller “catches” an http request, a new instance of the LoginProccesor bean is embedded (judging by the hash codes)? Although it has a scope within the request, but the controller itself has a Singleton scope… That is, the controller was created once -> its constructor was called once -> an instance of loginProccesor was also created once and immediately implemented
package com.example.testproject.controllers;
import com.example.testproject.models.LoginProccesor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class LoginController {
private final LoginProccesor loginProccesor;
public LoginController(LoginProccesor loginProccesor) {
this.loginProccesor = loginProccesor;
}
@GetMapping("/")
public String loginGet() {
return "login.html";
}
@PostMapping("/")
public String loginPost(
@RequestParam String username,
@RequestParam String password,
Model model) {
if (loginProccesor.login(username, password)) {
model.addAttribute("message", "Logged");
return "redirect:/main";
} else {
model.addAttribute("message", "Error!");
return "login.html";
}
}
}
Михаил Лоренсов is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.