Results 1 to 5 of 5

Thread: Newbie question : Does container pool beans?

  1. #1

    Default Newbie question : Does container pool beans?

    Hi all,

    If my Spring app is deployed in an application server like WAS or JBoss, and in it I have a servlet with the following code...


    Code:
    ServletContext servletContext =this.getServletContext();
    WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    
    Job job = (Job) applicationContext.getBean("oracleJob");
    Since this servlet is always running, will Spring create a new instance of the Job bean or does it maintain a pool of beans from which it serves?

    Also this specific bean is for Spring Batch but I do not think that should just change how the Spring container treats it.

    Thanks in advance for any help.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default


    Since this servlet is always running, will Spring create a new instance of the Job bean or does it maintain a pool of beans from which it serves?
    Neither... It returns a singleton... I suggest a read of the reference guide (chapter 3) which explains that.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hi,
    It ll not create a pool it depends on you when you creating the bean you can set scope in that bean according to that scope,
    the bean get created when needed.

    spring framework
    Last edited by abani; Dec 18th, 2011 at 01:22 AM.

  4. #4

    Default

    Hi Marten,

    If it is a singleton, and since servlets are not thread safe, then is there some concurrency issues if multiple requests come into the servlet at the same time? Will the same bean be shared across multiple requests at one time?

    Thanks.

  5. #5
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    If it is a singleton, and since servlets are not thread safe, then is there some concurrency issues if multiple requests come into the servlet at the same time? Will the same bean be shared across multiple requests at one time?
    If your bean is stateless, then there's not going to be any problem. If it is stateful, then obviously you can't use singleton which is Spring default; so when you configure the bean (either with xml or by annotations) you should specify that you want prototype scope. I suggest a read of the reference guide.

    On a side note, if you used Spring Web as your front-end framework you could also use two better suited scopes which are tailored for web applications; the session scope and the request scope.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •