I need to define a string value in Spring context XML file that is shared by multiple beans.
This is how I do it:
<bean id="aSharedProperty" class="java.lang.String">
<constructor-arg type="java.lang.String" value="All beans need me :)"/>
</bean>
Creating a java.lang.String bean by passing a constructor argument of java.lang.String seems kludgy.
Is there a shortcut?
I know this property can be passed using PropertyOverrideConfigurer, but I want to keep this property within the XML file.
You can use
PropertyPlaceholderConfigurer
and keep values in xml:Then you reference it with:
Something I've used in the past is SpEL to make sure that a bean has the same value as another:
I have found this to be particularly useful when setting the value did something other than a simple assignment.
You may be able to use the following:
However, that relies on the property having the same name, so may not be applicable for you.
A shorthand to the solution proposed by mrembisz goes like this: