Announcement Announcement Module
Collapse
No announcement yet.
Why only static methods allowed on factory-bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Re: Why only static methods allowed on factory-bean?

    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

    Comment


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

      Comment


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

        Comment


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

          Comment


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

            Comment


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

              Comment


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

                Comment

                Working...
                X