Spring Expression Language (SpEL) Predefined Variables
Join the DZone community and get the full member experience.
Join For FreeSpring 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/
Opinions expressed by DZone contributors are their own.
Comments