A matter of expression
Spring 3 will also offer an expression language (EL). Similar approaches are, for example, widely used in web development. An expression language uses expressions that are executed at runtime, allowing developers to implement dynamic system elements, without having to use complex programming languages.
While the introduction of an expression language was triggered by the requirements of other projects, like Spring Web Flow, it also offers a crucial advantage for writing Spring configurations. Until now, developers were limited to using fixed values. Experience gained in a similar approach with Spring for .Net has shown that an expression language offers significant advantages.
EL expressions can be implemented both in the XML configuration and with annotations. The following listing shows an example.
@Component
public class CountryAwareLogic {
...
@Autowired
public void setCountry(
@Value("#{systemProperties.country}") String country) {
this.country=country;
}
}
The @Component
annotation automatically instantiates the class as a Spring bean. Because of the @Autowired
annotation, the Spring container automatically calls the setCountry()
method. Now, @Value
can inject a parameter value. In our concrete example, it is a Java system property that can be passed to the Java Virtual Machine (JVM) using -D
. Other variables, like the Spring beans, can be accessed at this point, and the approach supports various different expressions, for example arithmetic expressions.
The example shows some of the advantages of using Spring EL: Access to components, like the system properties, is simplified significantly, and it is part of a general concept that also accepts other sources of variables. For annotations the configuration becomes more flexible and more convenient in practical use, because it allows values to be configurable – for example through system properties. Previously, annotations could only be used to configure Spring beans and their relationships, but not primitive values.
With these properties, Spring's configuration options suitably complement and extend the introduction of the expression language.