How can be spring bean with factory-method but without factory?

24,861

Solution 1

From docs

the constructor arguments specified in the bean definition will be used to pass in as arguments to the constructor of the ExampleBean. Now consider a variant of this where instead of using a constructor, Spring is told to call a static factory method to return an instance of the object:

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    // a private constructor
    private ExampleBean(...) {
      ...
    }

    // a static factory method; the arguments to this method can be
    // considered the dependencies of the bean that is returned,
    // regardless of how those arguments are actually used.
    public static ExampleBean createInstance (
            AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {

        ExampleBean eb = new ExampleBean (...);
        // some other operations...
            return eb;
    }
}

Note that arguments to the static factory method are supplied via constructor-arg elements, exactly the same as if a constructor had actually been used. Also, it is important to realize that the type of the class being returned by the factory method does not have to be of the same type as the class which contains the static factory method, although in this example it is. An instance (non-static) factory method would be used in an essentially identical fashion (aside from the use of the factory-bean attribute instead of the class attribute), so details will not be discussed here.

Solution 2

It simply means that com.test.checkDate has a static method called getPreviousDate. Your factory creating the objects is com.test.checkDate. We don't know which type is the object returned, is not said in the configuration, probably is java.util.Date.

the definition does not specify the type (class) of the returned object, only the class containing the factory method.

The constructor-arg are simply passed as parameters to getPreviousDate. Since the method is static, it doesn't need an instance of checkDate. If it sounds funny to use constructor-arg for calling a method that is not technically a constructor, think that the static method is indeed constructing an Object, so it will be easier to remember.

Since in an earlier version of your answer you mentioned "without factory", maybe you're thinking about the case of instantiation using an instance factory method, which requires the factory-bean attribute, but this is the case of Instantiation with a static factory method.

Share:
24,861
user710818
Author by

user710818

Updated on November 01, 2020

Comments

  • user710818
    user710818 over 3 years

    After investigation of code I found:

     <bean id="TestBean" class="com.test.checkDate"
     factory-method="getPreviousDate">
     <constructor-arg value .............
    
     ...............................
    

    How it can be possible? Thanks.

  • Lahniep
    Lahniep over 10 years
    Thanks ... you saved me some precious time.