使用 spring 中的方法返回的对象注入构造函数
Injecting a constructor with an object returned by a method in spring
在我的spring申请中-config.xml我有
<bean id=“shapeFactory" class="com.shapes.ShapeFactory" />
<bean id=“shapeHelper" class="com.shape.ShapeHelper">
<constructor-arg value=“#{shapeFactory.createDefaultShape()}” />
</bean>
但是在我的 shapeFactory class 中,我还有一个方法,其签名类似于
public Shape createShape(final Collection<Class<? extends Throwable>>,
final Collection<Class<? extends Throwable>> ,
final double ,
final double ,
final int , final double)
如何通过调用将不同参数作为参数的 createShape 来实例化 shapeHelper 对象。
编辑:为了简化示例,假设我有
public Shape createShape(final double ,
final double ,
final int , final double)
我怎么能调用上述任何方法并使 returnShape 成为 shapeHelper 的构造函数的参数?
一种方法是使用工厂主体创建名为 shape 的 bean
<bean id="shape " class="..Shape" factory-bean="shapeFactory" factory-method="createDefaultShape">
<constructor-arg name="argNAme" type="java.lang.Long" value="1">
<!--all args go here ? THEY WILL passed as parameters to the method createDefaultShape -->
</constructor-arg>
然后在你的 shapeHelper 中你可以简单地引用那个 bean
<bean id=“shapeHelper" class="com.shape.ShapeHelper">
<constructor-arg ref="shape" />
</bean>
在我的spring申请中-config.xml我有
<bean id=“shapeFactory" class="com.shapes.ShapeFactory" />
<bean id=“shapeHelper" class="com.shape.ShapeHelper">
<constructor-arg value=“#{shapeFactory.createDefaultShape()}” />
</bean>
但是在我的 shapeFactory class 中,我还有一个方法,其签名类似于
public Shape createShape(final Collection<Class<? extends Throwable>>,
final Collection<Class<? extends Throwable>> ,
final double ,
final double ,
final int , final double)
如何通过调用将不同参数作为参数的 createShape 来实例化 shapeHelper 对象。
编辑:为了简化示例,假设我有
public Shape createShape(final double ,
final double ,
final int , final double)
我怎么能调用上述任何方法并使 returnShape 成为 shapeHelper 的构造函数的参数?
一种方法是使用工厂主体创建名为 shape 的 bean
<bean id="shape " class="..Shape" factory-bean="shapeFactory" factory-method="createDefaultShape">
<constructor-arg name="argNAme" type="java.lang.Long" value="1">
<!--all args go here ? THEY WILL passed as parameters to the method createDefaultShape -->
</constructor-arg>
然后在你的 shapeHelper 中你可以简单地引用那个 bean
<bean id=“shapeHelper" class="com.shape.ShapeHelper">
<constructor-arg ref="shape" />
</bean>