Announcement Announcement Module
Collapse
No announcement yet.
how to use static method of a bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to use static method of a bean

    i know that when you use getBeanName methods means to create a new object . but i wonder how to use static method in a bean.

  • #2
    If the bean has singleton status within the container you just get the same object.

    Comment


    • #3
      Code:
      public class BaseObjectFactory{
      public static BaseObject createInstance(String type){
          if (type.equals("a"))
          return new ABaseObjce();
          if (type.equals("b"))
          return new BBaseObject();
      }
      }
      ABaseObject and BBaseObject are subclass of BaseObject.how to use creatInstance methods under spring container.Thanks

      Comment


      • #4
        If you want to invoke static methods from within the configuration file you might have a look at MethodInvokingFactoryBean. In the API documentation of that class you will find an example of its usage.

        Hope that helps,
        Andreas

        Comment


        • #5
          i found the javadoc of MethodInvokingFactoryBean
          "This use case is not supported by factory-methods, since a return value is needed to become the bean."
          in my example, ABaseObject and BBaseObject also have bean name in my applicationcontext.xml. i wonder if this static methods can return bean correctly? from doc, this class can not do that!

          Comment


          • #6
            The phrase you are citing refers to the use-case of calling initialization methods (which have no return value). This is not supported by factory methods (i.e. "normal" BeanFactory mechanisms), but it is supported by usage of MethodInvokingFactoryBean. I do not see a connection to the use case you are describing. So you might give it a try.

            Regards,
            Andreas

            Comment

            Working...
            X