1

In an applicationcontext.xml, is it possible to set a value which can be used later in SPEL expressions?

For example is there a way to do this?:

<setProperty name="foo" value="someval" />


<bean id="beanId" name="beanName" class="SomeClass">
    <property name="someVal" value="blah_${foo}"/>
</bean>

The actual reason I want to do this is that I use statements to create entity managers which are used in many different application contexts. The problem is that the entity managers require a unique name which is used by Bitronix to create a local file which breaks if multiple unit tests run at the same time using the same name for that field. To set that unique name I currently have a separate properties file for each application context and import it to get a unique name from it.

Rather than doing that nonsense I'd rather just do this:

<setProperty name="uniqueName" value="someUniqueName" />
<import resource="classpath*:shared/db/fooDb.xml" />
4

1 回答 1

5

You can do this using Spring-el and util namespace:

<util:properties id="myprops">
    <prop key="foo">someval</prop>
</util:properties>

<bean id="beanId" name="beanName" class="SomeClass">
    <property name="someVal" value="blah_#{myprops.foo}"/>
</bean>
于 2012-10-17T23:10:41.360 回答