Friday 25 March 2011

Spring 3.0 with EL

Spring 3.0 introduces support for expression language, which is similar to Unified EL support in jsp. The intention was to further provide different ways of setting bean properties.
The advantage of EL is that it can support different kinds of expressions like Boolean, literal ,regular , method invocation, etc.

It is also called spEL or spring EL.
Now again there are 2 methods of using EL notation - xml and annotations.

Example :
public class ErrorHandler {

private String defaultLocale;

public void setDefaultLocale(String defaultLocale) {
this.defaultLocale = defaultLocale;
}

public void handleError() {
//some error handling here which is locale specific
System.out.println(defaultLocale);
}
}

Using EL in xml  config :

<bean id="errorHandler" class="xxxx.ErrorHandler">
<property name="defaultLocate" value="#{systemProperties['user.region']}" />
</bean>

Annotation style config:

import org.springframework.beans.factory.annotation.Value;

public class ErrorHandler {

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

public void handleError() {
//some error handling here which is locale specific
System.out.println(defaultLocale);
}
}
Prior to Spring 3.0 , the only way to provide configuration metadata was XML.

No comments:

Post a Comment