tags:

views:

1089

answers:

3

I'd like to create a spring bean that holds the value of a double. Something like:

<bean id="doubleValue" value="3.7"/>
+1  A: 

Why don't you just use a Double?

ScArcher2
+5  A: 

Declare it like this

<bean id="doubleValue" class="java.lang.Double">
    <constructor-arg index="0" value="3.7"/>
</bean>

And use like this

<bean id="someOtherBean" ...>
    <property name="value" ref="doubleValue"/>
</bean>
Pavel Feldman
+2  A: 

It's also worth noting that depending on your need defining your own bean may not be the best bet for you.

<util:constant static-field="org.example.Constants.FOO"/>

is a good way to access a constant value stored in a class and default binders also work very well for conversions e.g.

<bean class="Foo" p:doubleValue="123.00"/>

I've found myself replacing many of my beans in this manner, coupled with a properties file defining my values (for reuse purposes). What used to look like this

<bean id="d1" class="java.lang.Double">
  <constructor-arg value="3.7"/>
</bean>
<bean id="foo" class="Foo">
  <property name="doubleVal" ref="d1"/>
</bean>

gets refactored into this:

<bean
  id="propertyFile"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
  p:location="classpath:my.properties"
/>
<bean id="foo" class="Foo" p:doubleVal="${d1}"/>
enricopulatzo