Results 1 to 8 of 8

Thread: Why only static methods allowed on factory-bean?

  1. #1
    Join Date
    Nov 2004
    Posts
    15

    Default Why only static methods allowed on factory-bean?

    Hello,

    I have a doubt with regard to the new factory-bean factory-method introduced in the 1.1 release.

    In general i find it a very good idea but i have found a problem while accessing a method from a bean defined in the container. Is there any reason so that the instantiateUsingFactoryMethod in the AbstractAutowireCapableBeanFactory only searches for static methods?

    I would understand this for static factory methods dfined in some classes, but im confused with the line

    if (Modifier.isStaticfactoryMethod.getModifiers()) &&

    Which forces static attribute on an already context instantiated bean.

    Thx for your time.

    Sergio.

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

    Default Re: Why only static methods allowed on factory-bean?

    Quote Originally Posted by Sergio
    In general i find it a very good idea but i have found a problem while accessing a method from a bean defined in the container. Is there any reason so that the instantiateUsingFactoryMethod in the AbstractAutowireCapableBeanFactory only searches for static methods?
    As I understand the documentation, that limitation is valid only if the BeanDefinition specifies a class. I understand it's the class to instantiate. So it would not make sense to allow for non-static instances for it would require to instantiate the type before invoking the method.

    Besides that you could use a FactoryBean for instantiation.

    Regards,
    Andreas

  3. #3
    Join Date
    Nov 2004
    Posts
    15

    Default it checks for static methods only

    I agree that the limitation for static methods should not apply to instantiated beans. My question was that nowadays in the container the code explicitly checks for static methods only.

    Is this a bug or a design decision?

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

    Default

    Just had a look into the code (version 1.1.1; maybe you have an older version).
    I think the line you are referring to is line 365:
    ... Modifier.isStatic(factoryMethod.getModifiers()) == isStatic ...

    Notice that the isStatic boolean flag is set to false when dealing with beanFactories. So the code is in fact referring to instance methods in that case.

    Regards,
    Andreas

  5. #5
    Join Date
    Nov 2004
    Posts
    15

    Default perfect with 1.1.1

    I had 1.1rc1, checked the latest version 1.1.1 and it was corrected there.

    Just one more question regarding the way it has been corrected.

    If i have read correctly the code then if we use a factory bean then we cannot invoke static methods included in the bean.

    Why do you impose this constraint?

    I mean, why dont you simply allow the execution of any public method (without static or not static check) in case of a factory-bean?

    Thx for your time, users are the worst problem of code!

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

    Default

    Actually I am no Spring developer, so I can only guess.
    I would say, the restriction has been made out of design purposes. The idea of a factory bean is actually the creation of an object. To additionally support static factory methods on a bean instance which itself is a factory seems not intuitive. So I do not view this as a real restriction in usage.

    Regards,
    Andreas

  7. #7
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Andreas, you've correctly surmised and summarized the design thinking there. Why create an instance if you only want to work with a static method? I think this would be rather misleading.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  8. #8
    Join Date
    Nov 2004
    Posts
    15

    Default I fully agree, just curious

    Thats what i thought about it,

    The only problem that migh arise is in the case of some 3rd party library where the method is declared as static but usually called in an object.

    But it usually represents a very bad design decissions and programmers should be spanked (for example changing a global static variable from constructor or any other method. The consecuences of allowing that on unprepared hands bring me nightmares, yeeeks!)

    Thx for your time,

    The more i use spring the more i like it .

Similar Threads

  1. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Replies: 1
    Last Post: Jul 28th, 2005, 05:08 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

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