Spring Beans - 如何将null作为构造函数arg连接?

IAm*_*aja 44 java spring

我定义了以下bean:

<bean id="myBean" class="com.me.myapp.Widget">
    <constructor-arg name="fizz" value="null"/>
    <constructor-arg name="buzz" ref="someOtherBean" />
</bean>
Run Code Online (Sandbox Code Playgroud)

当我运行我的应用程序时,Spring会抛出一个bean配置异常:

[java] Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [spring-config.xml]:
Unsatisfied dependency expressed through constructor argument with index 0
of type [com.me.myapp.Widget]: Could not convert constructor argument value of
type [java.lang.String] to required type [com.me.myapp.Widget]: Failed to
convert value of type 'java.lang.String' to required type
'com.me.myapp.Widget'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String] to required
type [com.me.myapp.Widget]: no matching editors or conversion strategy found
Run Code Online (Sandbox Code Playgroud)

我只是想创建一个Widget像我写的以下Java:

Widget w = new Widget(null, someOtherBean);
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?提前致谢!

Rei*_*eus 85

你可以使用Spring的<null>标签:

<bean id="myBean" class="com.me.myapp.Widget">
    <constructor-arg name="fizz">
            <null />
    </constructor-arg>
    <constructor-arg name="buzz" ref="someOtherBean" />
</bean>
Run Code Online (Sandbox Code Playgroud)