Results 1 to 5 of 5

Thread: Accessing system properties in SpEL programmatically

  1. #1
    Join Date
    Jan 2007
    Posts
    107

    Question 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 at 03:22 PM.

  2. #2

    Default 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?

  3. #3
    Join Date
    Jan 2007
    Posts
    107

    Default

    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.

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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!
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Jan 2007
    Posts
    107

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •