Intentionally setting a Spring bean to null

后端 未结 7 1908
孤独总比滥情好
孤独总比滥情好 2020-12-05 06:32

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

相关标签:
7条回答
  • 2020-12-05 07:37

    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
    
    0 讨论(0)
提交回复
热议问题