Reputation: 10458
I can create an object from a static factory object like so:
<bean id="myFactory" class="com.myco.MyFactory1" factory-method="createFactory">
<constructor-arg value="aString" />
</bean>
Now I want to use the id 'myFactory' to call its static 'createFactory' method.
In java something like:
ObjectFactory objectFactory = MyFactory.createFactory().createFactory(); //Last createFactory method returns type ObjectFactory
Upvotes: 0
Views: 1122
Reputation: 759
You can call factory methods on bean instances, but in this case the factory method must not be static.
<bean id="myFactory2" factory-bean="myFactory" factory-method="createFactory">
</bean>
Anyway, I agree to the others. You need to specify the requirement in detail. If your first com.myco.MyFactory1 returns an instance of com.myco.MyFactory1, it does not make much sense to call its createInstance method. As the same class is returned, the same static method will be called.
If your first factory com.myco.MyFactory1 returns instances of different classes, you can use the code from above to call factory-methods on these. But remember they have to be non static in this case.
UPDATE:
It is possible to create the new instance by calling a static method on another bean instance as follows.
<bean id="myFactory2" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="myFactory"/></property>
<property name="targetMethod"><value>createFactory</value></property>
</bean>
Nevertheless you should perhaps rethink your design.
Upvotes: 2
Reputation: 597114
That is invalid Java code. It assumes your MyFactory
has two methods with the same name - one static
and one non-static. And this is not possible.
In case you are invoking the same static method twice, or invoking a method on another object - well, you can easily invoke it only once / merge the two invocations, into a, say createObjectFactory()
Furthermore, it seems really odd to create a factor which in turn creates a factory which in turn creates another factory. Sounds like a bad design (overdesign).
Upvotes: 2