Home  >  Spring Core

Spring Expression Language Support

By Arvind Rai, February 11, 2013
Spring provides expression language support in XML and Annotation based both. The format of expression language is #{ <expression string> }.

XML Based Spring Expression Language Support

Find the random number in spring by spring expression language support.

ExpressionVO.java
package com.concretepage;
public class ExpressionVO {
	private int randomNumber;

	public int getRandomNumber() {
		return randomNumber;
	}

	public void setRandomNumber(int randomNumber) {
		this.randomNumber = randomNumber;
	}
}
 
expressiontest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  
<bean id="expression" class="com.concretepage.ExpressionVO">
   <property name="randomNumber" value="#{ T(java.lang.Math).random() * 10}"/>
</bean>

</beans>
 
SpringExpressionTest.java
package com.concretepage;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.concretepage.dao.PageDao;

public class SpringExpressionTest {
    public static void main(String... args) {
    	AbstractApplicationContext context = new ClassPathXmlApplicationContext("expressiontest.xml");
    	ExpressionVO ex=(ExpressionVO) context.getBean("expression");
        int rn=ex.getRandomNumber();
        System.out.println(rn);
    } 
}
 



Annotation Based Spring Expression Language Support

To get annotation based spring expression language support, we need to do changes in two file. @Value annotation is applied on bean. @Value belongs to org.springframework.beans.factory.annotation.

ExpressionVO.java
package com.concretepage;
import org.springframework.beans.factory.annotation.Value;

public class ExpressionVO {
	
	@Value("#{ T(java.lang.Math).random() * 10}")
	private int randomNumber;

	public int getRandomNumber() {
		return randomNumber;
	}

	public void setRandomNumber(int randomNumber) {
		this.randomNumber = randomNumber;
	}
}
 
expressiontest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 <context:annotation-config/>
<bean id="expression" class="com.concretepage.ExpressionVO">
</bean>

</beans>
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us