Gordon Dickens is an instructor, mentor & consultant. Gordon is currently architecting and teaching several official SpringSource courses and actively tweets about open source technology at http://twitter.com/gdickens. Gordon is active within the Spring Framework community focussed on: Spring training, Spring Roo, Spring Integration, Spring Batch and Eclipse Virgo OSGi projects. Gordon is a DZone MVB and is not an employee of DZone and has posted 39 posts at DZone. You can read more from them at their website. View Full User Profile

Spring Expression Language (SpEL) Predefined Variables

05.15.2011
| 10632 views |
  • submit to reddit

Spring 3.0 introduced Spring Expression Language (SpEL). There are two variables that you have available “systemProperties” and “systemEnvironment“. SpEL allows us to access information from our beans and system beans information at runtime (late binding).

These can be applied to bean fields as defaults using the JSR-303 @Value annotation on a field or in XML with the <bean ... value="" /> options.

  • systemProperties – a java.util.Properties object retrieving properties from the runtime environment
  • systemEnvironment – a java.util.Properties object retrieving environment specific properties from the runtime environment

We can access specific elements from the Properties object with the syntax:

  • systemProperties['property.name']
  • systemEnvironment['property.name']

public class MyEnvironment {

@Value("#{ systemProperties['user.language'] }")
private String varOne;

@Value("#{ systemProperties }")
private java.util.Properties systemProperties;

@Value("#{ systemEnvironment }")
private java.util.Properties systemEnvironment;

@Override
public String toString() {
return "\n\n********************** MyEnvironment: [\n\tvarOne="
+ varOne + ", \n\tsystemProperties=" + systemProperties
+ ", \n\tsystemEnvironment=" + systemEnvironment + "]";
}
}

Register the “MyEnvironment” bean in your Spring context and create a JUnit test to display the variables.

 

From http://gordondickens.com/wordpress/2011/05/12/spring-expression-language-spel-predefined-variables/

Published at DZone with permission of Gordon Dickens, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: