Is there a new bean object created with every request in spring mvc?

java, spring, spring-mvc

Solution

Spring's application context (the bean IoC container, which is responsible for managing bean lifecycle from its instantiation to its destruction) contains bean definitions. These definitions among other attributes contain so called `scope`. This scope can have the following values:

- `singleton` - only one instance of the bean is created during the application lifetime

- `prototype` - every time someone asks (`applicationContext.getBean(...)`) for this bean a new instance is created

You can have some special scopes as well:

- `request` - bean lifecycle is bound to the HTTP request

- `session` - bean lifecycle is bound to the HTTP session

You can even create your own scopes. The default scope for beans is `singleton`. So if you don't specify otherwise, the bean is `singleton` (single instance per application).

If you are using `component-scan` which searches for classes annotated with `@Component`-like annotations (e.g. `@Controller`), then these classes are automatically registered as bean definitions in application context. The default scope applies to them as well.

If you want to alter scope of these auto-registered beans, you have to use `@Scope` annotation. Check its JavaDoc if you are interested in how to use it.

TL;DR Your `LoginController` is `singleton`.

Problem

I am not able to understant that only one bean object is created in spring mvc using dispatcher servlet or a new object gets created with every request? Controller code:- In the code i am setting the data in the LoginBean Object and setting it in the modelandview object in method abc. Then in the jsp i am not entering any value for usename, in that case when i submit the form and when the handler method(initform) is called then i am trying to print the same lb.getusername which is not returing me any value. Not able to understand the concept. ``` @Controller public class LoginController{ ModelAndView mv=null; EmployeeBean e=new EmployeeBean(); AutoBean autobean; @Autowired public LoginController(AutoBean autobean){ System.out.println("autobean"); this.autobean=autobean; } @RequestMapping(value="/login") public ModelAndView abc(){ System.out.println("here"); System.out.println("here1"); LoginBean lb=new LoginBean(); lb.setUsename("ankita");//setting value return new ModelAndView("login","loginbean",lb); } @RequestMapping(value="/abc1",method=RequestMethod.POST) public ModelAndView initform(@ModelAttribute("loginbean")LoginBean lb,BindingResult result,Model model){ System.out.println("*****"+result.getErrorCount()); System.out.println("hello"); autobean.setName("yayme"); System.out.println(autobean.getName()); model.addAttribute("autobean", autobean); System.out.println("username"+lb.getUsename());// query?? if(lb.getPassword().equals("ankita")) /*{ mv=new ModelAndView(); e.setId("1001"); e.setName("ankita"); mv.addObject("employee", e); mv.addObject("emp", new Emp()); mv.setViewName("success"); return mv; }*/ return new ModelAndView("success","emp",new Emp()); else return new ModelAndView("fail","lb1",lb); } ``` login.jsp ``` <form:form action="abc1" commandName="loginbean"> username:<form:input path="usename" /> password:<form:password path="password"/> <input type="submit"/> </form:form> ``` Please suggest?

Original source