Tags

, , ,

Though it is pretty old way, we will see how to utilize spring expression language in the convenient places.

Spring Expression Language

spEL is powerful and gives a way to resolve the expressions in spring config itself. Also, It is used to notate literal values int, string, engineering notation etc.

Consider the below example,

<beans:bean id=”invokeHandler1″ class=”com.murali.test.xa.InvokeTestHandler”>
<beans:property name=”intVal” value=”#{5}” />
<beans:property name=”intVal2″ value=”5″ />
</beans:bean>

This set the intVal variable to integer value 5 in InvokeTestHandler bean. You can achieve the same without spring expression language since as we know that spring does the autoboxing automatically like intVal2.

The real benefit of spEL comes when you resolve the expressions using spEL. Below are some examples.

Case 1: Referrring to another bean’s property using spring expression language

You can use spEL to refer another bean’s propery as like below.

<beans:bean id=”testHandler” class=”com.murali.test.xa.TestHandler”>
<beans:property name=”val” value=”TEST” />
</beans:bean>

<beans:bean id=”invokeHandler1″ class=”com.murali.test.xa.InvokeTestHandler”>
<beans:property name=”val” value=”#{testHandler.val}” />
</beans:bean>

In java, the above is equal to,

InvokeTestHandler handle = new InvokeTestHandler();
handle.setVal(testHandler.getVal());

Case 2:

<beans:bean id=”testHandler” class=”com.murali.test.xa.TestHandler”>
<beans:property name=”val” value=”TEST” />
</beans:bean>

<beans:bean id=”invokeHandler1″ class=”com.murali.test.xa.InvokeTestHandler”>
<beans:property name=”resolveExp” value=”#{testHandler.getVal().toLowerCase()}” />
</beans:bean>

When you run the above code, it will set “test” to getResolveExp() of your InvokeTestHandler bean. This is very simple example. You can resolve complex expressions also in this same way.

Case 3:

You may possibily get some exceptions when you resolve the expressions in spring config. For example, you will get nullPointerException in the above example if getVal() returns null.

<beans:bean id=”testHandler” class=”com.murali.test.xa.TestHandler”>
<beans:property name=”val”>
<beans:null />
</beans:property>
</beans:bean>

<beans:bean id=”invokeHandler1″ class=”com.murali.test.xa.InvokeTestHandler”>
<beans:property name=”resolveExp” value=”#{testHandler.getVal().toLowerCase()}” />
</beans:bean>

It throws null pointer exception as “Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1011E:(pos 21): Method call: Attempted to call method toLowerCase() on null context object”.

To avoid this, you can use the below notation for null pointer check.

<beans:bean id=”invokeHandler1″ class=”com.murali.test.xa.InvokeTestHandler”>
<beans:property name=”resolveExp” value=”#{testHandler.getVal()?.toLowerCase()}” />
</beans:bean>

if getVal() is null, it returns null to getResolveExp() method in InvokeTestHandler.

Case 4: Access to static methods and constants

T() notation is used to evaluate static methods and constants in spring config file.

<property name=”multiplier” value=”T(java.lang.Math).PI” />

It sets the constant value PI to mulitplier variable.

To wire a random number (between 0 and 1) into a bean property,

<property name=”randomNumber” value=”T(java.lang.Math).random()” />

To wire the randomNumber property, it will use Math.random() method to determine the value for that property.

Case 5: Resolving match expressions

When working with text, it’s sometimes useful to check to see if that text matches a certain pattern. SpEL supports pattern matching in expressions with its matches operator.

The result of a matches evaluation is a boolean value; true if the value matches the regular expression, false otherwise.

To demonstrate the matches operator, suppose that we want to check to see if a String contains a valid email address. In that case, we could apply the matches operator like this:

<property name=”validEmail” value= “#{admin.email matches ‘[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}’}”/>

Also, you load the properties file and get a value of key from properties file like below.

<util:properties id=”settings” location=”classpath:settings.properties” />
<property name=”url” value=#{settings[‘test.URL’]}”/>

test.URL is the key in settings.properties file. It sets the value for test.URL key to url variable.

 

Advertisements