PDA

View Full Version : what to do if a factory-method returns null?



Injecteer
Dec 30th, 2005, 05:13 AM
Hi gurus :)

in my appContext.xml there's a factory method, that extracts a configuration object out of a DB.

<bean id="contentProvider" class="com.ect.ringBackTones.dao.ContentProviderDAO" factory-method="getEctContentProvider"/>

if the DB is empty, then the method naturally returns null. That causes the Spring's ApplicationContext to cry-out complaining about the exception and die:

org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'contentProvider' defined in class path resource [applicationContextRBT.xml]: Factory method 'getEctContentProvider' on class [com.ect.ringBackTones.dao.ContentProviderDAO] returned null


of course, I can make sure, that the method returns a not-null dummy, but are there a respective Spring's functionality to ignore null-beans, so that the responsibility to validate such beans is shifted to the class, where the null gets injected?

ps. I tried to look for some clues in the docs, as well as in the forum but couln't dinf anything similar :confused:

Rod Johnson
Dec 30th, 2005, 05:35 AM
Spring assumes that all factory methods return non-null. It would be interesting to know if more people see a demand for accepting-null behaviour.

Injecteer
Dec 30th, 2005, 06:50 AM
Spring assumes that all factory methods return non-null. It would be interesting to know if more people see a demand for accepting-null behaviour.
thanx for the reply. I thought, I won't get any until the end of new year celebrations :p

About the topic... I think it would be just nice to have a possibility to control BeanFactory's reaction on nulls. So that one can declare on a bean or context basis, that the framework just reports about the null, and proceeds with loading the rest of the context or breaks the init, as it's doing now.

I mean, if the Spring allows a <null/> dummy to be injected, then it would be logical to have a 'lazy null check' on beans as well.

Colin Sampaleanu
Dec 30th, 2005, 04:08 PM
In the case where you want to call a method on a class, that may or may not return a value, you could use MethodInvokingFactoryBean instead. In the case of a null, that will return
MethodInvoker.VoidType (http://www.springframework.org/docs/api/org/springframework/util/MethodInvoker.VoidType.html)However, I don't think that's going to help you here, as you want to inject the return value as void, not this special marker type...

You can't subclass it either, as FactoryBeans need to return something, which is why the marker type is returned in the first place.

Regards,