Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 294 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does Spring 3 expression language interact with property placeholders?

#1
Spring 3 has introduced a new [expression language][1] (SpEL) which can be used in bean definitions. The syntax itself is fairly well specified.

What isn't clear is how, if at all, SpEL interacts with the property placeholder syntax that was already present in prior versions. Does SpEL have support for property placeholders, or do I have to combine the syntax of both mechanisms and hope they combine?

Let me give a concrete example. I want to use the property syntax `${x.y.z}`, but with the addition of "default value" syntax as provided by the [elvis operator][2] to handle cases where `${x.y.z}` is undefined.

I've tried the following syntaxes without success:

* `#{x.y.z?:'defaultValue'}`
* `#{${x.y.z}?:'defaultValue'}`

The first one gives me

> Field or property 'x' cannot be found
> on object of type
> 'org.springframework.beans.factory.config.BeanExpressionContext'

which suggests that SpEL doesn't recognise this as a property placeholder.

The second syntax throws an exception saying that the placeholder is not recognised, so the placeholder resolver *is* being invoked, but is failing as expected, since the property is not defined.

The docs make no mention of this interaction, so either such a thing is not possible, or it's undocumented.

Anyone managed to do this?


----------

OK, I've come up with a small, self-contained test case for this. This all works as-is:

First, the bean definitions:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

[To see links please register here]

">

<context:property-placeholder properties-ref="myProps"/>

<util:properties id="myProps">
<prop key="x.y.z">Value A</prop>
</util:properties>

<bean id="testBean" class="test.Bean">
<!-- here is where the magic is required -->
<property name="value" value="${x.y.z}"/>

<!-- I want something like this
<property name="value" value="${a.b.c}?:'Value B'"/>
-->
</bean>
</beans>

Then, the trivial bean class:

package test;

public class Bean {

String value;

public void setValue(String value) {
this.value = value;
}
}

And lastly, the test case:

package test;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PlaceholderTest {

private @Resource Bean testBean;

@Test
public void valueCheck() {
assertThat(testBean.value, is("Value A"));
}
}

The challenge - to come up with a SpEL expression in the beans file which allows me to specify a default value in cases where `${x.y.z}` cannot be resolved, and this default *must* be specified as part of the expression, not externalized in another property set.

[1]:

[To see links please register here]

[2]:

[To see links please register here]




Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through