Results 1 to 9 of 9

Thread: MethodInvokingFactoryBean problem w singleton

  1. #1
    Join Date
    Jul 2006
    Posts
    5

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Can you post your config please.
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  3. #3
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote 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.
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  5. #5
    Join Date
    Jul 2006
    Posts
    5

    Default

    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>

  6. #6
    Join Date
    Jul 2006
    Posts
    5

    Default

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

  7. #7
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote 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
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  8. #8
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote 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

  9. #9
    Join Date
    Jul 2006
    Posts
    5

    Default

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

Posting Permissions

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