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– ajava.util.Propertiesobject retrieving properties from the runtime environmentsystemEnvironment– ajava.util.Propertiesobject 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}")
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
Jorge,
That is now part of the Environment bean which you can autowire into your Spring classes & tests.
@AutowiredEnvironment environment