tags:

views:

329

answers:

3

Hello everybody,

I'm using Spring to inject JMS connection factory into my Java application. Since this factory is only required within the production environment, not while I'm developing though, I put the bean definition into a separate XML which I include into my main applicationContext.xml. In production environments this extra file contains the regular bean definition. In my local dev environment I'd like this bean to be null. Trying to simply remove the bean definition all-toghether obviously caused an error when Spring came across a reference ID it didn't know.

So I tried creating a factory bean that would simply return null. If I do this, Spring (2.5.x) complains that the factory returned null although based on the Spring API doc of the FactoryBean interface I expected this to work (see Spring API doc).

The XML looks something like this:

<bean id="jmsConnectionFactoryFactory" class="de.airlinesim.jms.NullJmsConnectionFactoryFactory" />

<bean id="jmsConnectionFactory" factory-bean="jmsConnectionFactoryFactory" factory-method="getObject"/>

I'm by no means a Spring expert. What would be the "correct" way of doing this?

Thank you very much!

A: 

Can you make use of the special <null> bean element ? e.g.

<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>

from the doc, section 3.3.2.5

Brian Agnew
This would probably work when setting the propery of the final application bean. But at this point I just provide the reference id "jmsConnectionFactory" which should then refer to either an actual bean or null (if that is possible at all).
Lunikon
+3  A: 

factory-bean/factory-method doesn't work with null, but a custom FactoryBean implementation works fine:

public class NullFactoryBean implements FactoryBean<Void> {

    public Void getObject() throws Exception {
        return null;
    }

    public Class<? extends Void> getObjectType() {
        return null;
    }

    public boolean isSingleton() {
        return true;
    }
}

-

<bean id="jmsConnectionFactory" class = "com.sample.NullFactoryBean" />
axtavt
Thank you very much, this did the trick!
Lunikon
There's an open feature request for this, but's been sitting there for 2 years... voting for it might help : http://jira.springframework.org/browse/SPR-5320
skaffman
This didn't work for me if I used the bean in an `@Autowired` context.
Alex Ciminian
A: 

I'm pretty sure that Spring won't allow you to associate null with a bean id or alias. You can handle this by setting properties to null.

Here's how you did this in Spring 2.5

<bean class="ExampleBean">
    <property name="email"><null/></property>
</bean>

In Spring 3.0, you should also be able to use the Spring expression language (SpEL); e.g.

<bean class="ExampleBean">
    <property name="email" value="#{ null }"/>
</bean>

or any SpEL expression that evaluates to null.

And if you are using a placeholder configurator you could possibly even do this:

<bean class="ExampleBean">
    <property name="email" value="#{ ${some.prop} }`"/>
</bean>

where some.prop could be defined in a property file as:

some.prop=null

or

some.prop=some.bean.id

Note, I haven't used SpEL (yet) so there might be problems with some of the above ...

Stephen C