Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intentionally setting a Spring bean to null

Tags:

java

spring

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"/>

What would be the "correct" way of doing this?

like image 791
Lunikon Avatar asked Jan 29 '10 16:01

Lunikon


People also ask

Can a bean be null?

Internal representation of a null bean instance, e.g. for a null value returned from FactoryBean. getObject() or from a factory method. Each such null bean is represented by a dedicated NullBean instance which are not equal to each other, uniquely differentiating each bean as returned from all variants of BeanFactory.

How do you inject null in Spring?

If you need to inject null value for any field in Spring then use the special <null/> element for it, don't use value=”null” because that will pass "null" as a String value. Use <null/> element instead in your Spring configuration.

Can we use @bean without @configuration?

@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.

Can @bean method be private?

The decision to go with package-private visibility was made simply because private visibility is not allowed for @Bean methods due to CGLIB constraints.


3 Answers

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
like image 163
Stephen C Avatar answered Oct 13 '22 20:10

Stephen C


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" />
like image 43
axtavt Avatar answered Oct 13 '22 21:10

axtavt


For anyone coming to this question, keep in mind that simply setting the @Autowired annotation as optional will do the trick (i.e. Spring will leave the reference null if no qualifying bean is found).

@Autowired(required = false)
private SomeClass someBean

Note that you would have to do this everywhere the bean is referenced, which may be a bigger hassle than creating a null-factory as mentioned above.

like image 29
Gerardo Lastra Avatar answered Oct 13 '22 21:10

Gerardo Lastra