The beans are defined thus:
<bean id="defaultSourceMetaData" class="com.example.metadata.SourceMetaData">
<constructor-arg type="java.lang.String" value="${source.name}"/>
</bean>
<bean id="reconSourceMetaData" class="com.example.metadata.SourceMetaData" factory-method="extractData">
<constructor-arg index="0" value="${source.metadata.query}"/>
</bean>
<bean id="sourceProvider" class="com.example.file.FileSourceProvider">
<property name="sourceMetaData" value="#{ reconSourceMetaData != null ? reconSourceMetaData : defaultSourceMetaData }"/>
</bean>
We know, that the reconSourceMetaData
is null — because the query initializing it returns an empty set. But the defaultSourceMetaData
is not being created — and the program fails with
Cannot convert value of type 'org.springframework.beans.factory.support.NullBean' to required type 'com.example.metadata.SourceMetaData' for property 'sourceMetaData': no matching editors or conversion strategy found
If I replace the SpEL-expression with a direct reference to the default bean:
<property name="sourceMetaData" ref="defaultSourceMetaData"/>
the defaultSourceMetaData
-bean is initialized, and the property is initialized correctly. But I do need the initialization to be conditional — what’s wrong with the syntax I’m using?