Results 1 to 9 of 9

Thread: Question about concurrency within a singleton property with request scope

  1. #1
    Join Date
    Oct 2012
    Posts
    7

    Default Question about concurrency within a singleton property with request scope

    Hi!

    I have a question about concurrency in a web application I hope someone could resolve for me.

    I have an application with a session destroyed listener X. Inside this listener there is an autowired singleton Y which also has another bean autowired inside it Z.
    My question is; if I have Z defined with request scope, when I make a call from X to Y, will it have a new instance of Z with each call or it will just have the same instance again and again?

    Thanks in advance!

  2. #2
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    To inject a request scoped bean into a singleton you should inject an aop scoped proxy, see

    http://static.springsource.org/sprin...ther-injection
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

  3. #3
    Join Date
    Oct 2012
    Posts
    7

    Default

    Thank you!
    That's exactly what I needed

  4. #4
    Join Date
    Oct 2012
    Posts
    7

    Default

    Another question related to this.
    Suppose this prototype bean is only used inside a method. What do you recommend?
    - Get the application context and obtained an instance of the prototype bean inside this method so there is no need for an instance variable
    - Create an aop scoped proxy as an instance variable to retrieve a real instance of the object with each call

    I don't know about perfomance. For me it seems easier and cleaner to just create the instance variable with autowire and use it as if it was a regular singleton but I guess it's more efficient the other way

  5. #5
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    Creating scoped proxies for prototypes is not a good idea, you will get a new instance on every method call. For example:

    Code:
    prototypeScopedProxyInstance.setName("Peter");
    boolean willBeFalse = "Peter".equals(prototypeScopedProxy.getName());
    As getName() is executed on a new instance...

    You can use ApplicationContext to get the prototype or use Lookup Method injection, if you want to keep decoupled your class from spring library.
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

  6. #6
    Join Date
    Oct 2012
    Posts
    7

    Default

    Ok, I've tried with lookup method injection and it works well. I had to rewrite the class to make it abstract and create the abstract method to get it overriden by spring. I tried creating a new method without changing the class to abstract but it doesn't work. For what I read in the documentation, if the class is not abstract, spring overrides the method you define in the lookup-method tag. If I don't change the class to abstract and also define the method abstract, when I call the method in my application, it uses the one defined in the class, no the one defined in the xml.
    Is it normal or did I do something wrong? Also, if I keep this class abstract but I create some other abstract methods I don't define inside a lookup-method tag, what does spring do with them? Does it create stub implementations of these abstract methods, returning null if the functions have to return some value?

  7. #7
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    No lookup or replaced methods are delegated to super implementation so them can not be abstract. A java.lang.AbstractMethodError will be throw when calling thems on the generated proxy

    The class don't need to be abstract for a lookup method, ensure that you are calling to a cglib proxy injected by container in your application and no to a instance created with new operator.
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

  8. #8
    Join Date
    Oct 2012
    Posts
    7

    Default

    My class definition is something like this

    <bean id="prototypeClass" class="package.PrototypeClass" scope="prototype" />

    <bean id="singletonClass" class="package.SingletonClass">
    <lookup-method bean="prototypeClass" name="getPrototypeClass"/>
    </bean>

    And the method definition is like this

    //Singleton class
    public class SingletonClass {

    ....
    private void someMethod(){
    ....
    PrototypeClass prototype = getPrototypeClass();
    ...
    }

    private PrototypeClass getPrototypeClass(){
    return null;
    }
    }
    If I don't change SingletonClass and getPrototypeClass method to abstract, when I call getPrototypeClass() I get a null pointer. Do I have to put something special inside getPrototypeClass method? If I don't define the class abstract I need to implement this method and it doesn't seem too clean just leaving it with a null returned value.
    Last edited by Kilian; Nov 19th, 2012 at 06:17 AM.

  9. #9
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    Make getPrototypeClass() public or protected to let container override it.
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

Posting Permissions

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