Announcement Announcement Module
Collapse
No announcement yet.
Accessing system properties in SpEL programmatically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing system properties in SpEL programmatically

    Hi,

    According to the docs this works.

    Code:
    <bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
        <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
    </bean>
    However accessing system properties programmatically in SpEL does not work.

    Code:
        @Test
        public void testGetSystemProperty() {
            System.setProperty("foo", "bar");
            ExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("systemProperties['foo']");
            String value = exp.getValue(String.class);
            assertEquals("bar", value);
        }
    What obvious piece of information am I missing? Is this predefined variable only registered on context load and if using programmatically this is something I have to register by myself?

    Code:
        @Test
        public void testGetSystemProperty() {
            System.setProperty("foo", "bar");
            EvaluationContext context = new StandardEvaluationContext();
            context.setVariable("systemProperties", System.getProperties());
            ExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("#systemProperties['foo']");
            String value = exp.getValue(context, String.class);
            assertEquals("bar", value);
        }
    Works but not quite the same

    Thanks.
    Last edited by Narada; Dec 25th, 2009, 03:22 PM.

  • #2
    might help

    the obvious thing I notice is, compared to your xml configuration, your SpEL in java code is not wrapped inside
    PHP Code:
    #{ ... } 
    . Could you verify if this might be the cause?

    Comment


    • #3
      Hi. Tried that already. That prefix and suffix should only be present in context files and not when using programmatically as far as I can tell. It allows Spring to identify SpEL syntax. If you use them programmatically SpEL throws an exception anyway. Thanks.

      Comment


      • #4
        As mentioned in another thread, Narada could you please raise an issue regarding this problem? It looks like a bug but AndyC will tell what the verdict is.

        Thanks!

        Comment


        • #5
          Done thanks.

          http://jira.springframework.org/browse/SPR-6651

          Comment

          Working...
          X