Spring Expression Language (SpEL) Predefined Variables

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.

Spring 3.2 improved this by simplifying these into one bean Environment

  • 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']



Define a bean (Spring 3.0):

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 + "]";
  }
}

Define a bean (Spring 3.2):

public class MyEnvironment {

  @Autowired
  Environment environment;

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

  @Override
  public String toString() {
    return environment.toString();
  }
}

Note: Spring Expression Language uses the pound sign “#” to indicate a bean reference.

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

To populate a variable from one of your properties from a properties file use: @Value("${some.property.of.mine}")

About Gordon

Technology enthusiast primarily focused on Java and Open Source projects. Spring Certified Professional and Trainer. http://twitter.com/gdickens http://linkedin.com/in/gordondickens http://github.com/gordonad
This entry was posted in SpEL, Spring, Spring Framework and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Spring Expression Language (SpEL) Predefined Variables

  1. Jorge says:

    But which would be valid properties keys to use on systemProperties and systemEnvironment? I haven’t found any documentation explaining this on chapter dedicated to SpEL on Spring’s documentation site :S

  2. Gordon says:

    Jorge,

    That is now part of the Environment bean which you can autowire into your Spring classes & tests.

    @Autowired
    Environment environment

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>