使用带有Spring变量参数的FactoryMethod创建Spring Bean?

res*_*ity 3 spring inversion-of-control

我们如何使用FactoryMethod变量参数创建bean .

 public class ConnectionFactoryClass {

    public static Connection composeConnection(final Property... properties) {
       ...
    }
 }
Run Code Online (Sandbox Code Playgroud)

bean.xml

  <bean id="Connection"
    class="com.example.ConnectionFactoryClass"
    factory-method="composeConnection"
    scope="singleton">
    <constructor-arg ref="Driver"/>
    <constructor-arg ref="Pool"/>
  </bean>
Run Code Online (Sandbox Code Playgroud)

春天给我一个错误说,

org.springframework.beans.factory.BeanCreationException:创建文件[./beans.xml]中定义名为'Connection'的bean时出错:找不到匹配的工厂方法:factory method'composeConnection'

ams*_*ams 5

请尝试以下方法:

<bean id="Connection"
    class="com.example.ConnectionFactoryClass"
    factory-method="composeConnection"
    scope="singleton">
    <constructor-arg> 
        <array>
        <bean ref="Driver" />
            <bean ref="Pool" />
        </array>
    </constructor-arg>
  </bean>
Run Code Online (Sandbox Code Playgroud)

我认为您遇到了问题,因为JVM将var arg参数转换为Object Array,您需要将单个参数传递给构造函数,该构造函数是对象数组.我没有尝试过上面的xml,所以我可能会有拼写错误,但上面的内容应该可行.