Announcement Announcement Module
Collapse
No announcement yet.
MethodInvokingFactoryBean problem w singleton Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MethodInvokingFactoryBean problem w singleton

    Hello,

    I would like my MethodInvokingFactoryBean to return a new instance each time the factory method is called. I saw that its implementation of getObject decides by looking at the attribute "singleton", if a new instanced is supposed to be created or not. Unfortunately I get this exception if I set the attribute 'singleton' to false:

    Error registering bean with name 'XYZ' [...] BeanDefinitionValidationException: FactoryBean must be defined as singleton - FactoryBeans themselves are not allowed to be prototypes

    My mistake or a bug? I use spring version 1.2.6.

    Cheers,

    Christopher

  • #2
    Can you post your config please.

    Comment


    • #3
      MethodInvokingFactoryBean invokes a method and returns the result. As far as I know, there is no caching involved. So there is no point in making the FactoryBean itself a prototype, as two factory beans would behave exactly the same.

      What exactly do you mean by "return a new instance" in context of invoking a method?

      Regards,
      Andreas

      Comment


      • #4
        Originally posted by Andreas Senft
        MethodInvokingFactoryBean invokes a method and returns the result. As far as I know, there is no caching involved. So there is no point in making the FactoryBean itself a prototype, as two factory beans would behave exactly the same.

        What exactly do you mean by "return a new instance" in context of invoking a method?

        Regards,
        Andreas
        Not quite If MIFB is set as a singleton it will cache the result of the method, if however, it is a prototype then every invocation of getObject will result in invoking the method.

        I expect for most implementations this is irrelevant, but if you (for example) had a method which returned an incremental integer then it would make a difference.

        Comment


        • #5
          I surely only want one instance of my factory bean. But: I want the factory bean to use new instances of my "targetObject" each time the factory method is called.

          As you can see the implementation of getObject of class "MethodInvokingFactoryBean" makes a difference if singleton is set to true or false:

          public Object getObject() throws Exception {
          if (this.singleton) {
          // Singleton: return shared object.
          return this.singletonObject;
          }
          else {
          // Prototype: new object on each call.
          Object retVal = doInvoke();
          return (retVal != null ? retVal : MethodInvoker.VOID);
          }
          }

          Can I set the singleton attribute by passing it by <property>? This is my configuration:

          <bean id="getMailHost" class="org.springframework.beans.factory.config.Me thodInvokingFactoryBean">
          <property name="targetObject" ref="globalsDao" />
          <property name="targetMethod">
          <value>getMailHost</value>
          </property>
          <property name="singleton" value="false" />
          </bean>

          Comment


          • #6
            @yatesco: only saw your post after I wrote mine. I think that's the point

            Comment


            • #7
              Originally posted by ccudennec
              I surely only want one instance of my factory bean. But: I want the factory bean to use new instances of my "targetObject" each time the factory method is called.
              Yes, there is some confusion between the "singleton" property of MIFB and the singleton attribute in the xml bean element.

              As you (and Adreas) say, you don't want to have a different instance of the factoryBean itself (so no need to set "singleton=false" on the bean element) but you do want every invocation of getObject to call the method.

              I believe the fragment you provided is correct:

              Code:
              <bean id="getMailHost" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                  <property name="targetObject" ref="globalsDao" />
                  <property name="targetMethod">
                    <value>getMailHost</value>
                  </property>
                  <property name="singleton" value="false" />
                </bean>[/QUOTE]
              Although I am not sure why you would want to call getMailHost multiple times???? But yes, that is the correct configuration.

              @yatesco: only saw your post after I wrote mine. I think that's the point
              Yes, I was replying to Andreas point

              Comment


              • #8
                Originally posted by yatesco
                Yes, I was replying to Andreas point
                Thanks for the pointer
                I didn't know that MIFB really does caching (never had a look at the sources).

                Regards,
                Andreas

                Comment


                • #9
                  Thanks for your help! Fastest answers in a forum I ever got!

                  Comment

                  Working...
                  X