Announcement Announcement Module
Collapse
No announcement yet.
Question about concurrency within a singleton property with request scope Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    To inject a request scoped bean into a singleton you should inject an aop scoped proxy, see

    http://static.springsource.org/sprin...ther-injection

    Comment


    • #3
      Thank you!
      That's exactly what I needed

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            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?

            Comment


            • #7
              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.

              Comment


              • #8
                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, 06:17 AM.

                Comment


                • #9
                  Make getPrototypeClass() public or protected to let container override it.

                  Comment

                  Working...
                  X