CodesJava

Easy learning with example program codes

Spring spel variable example


Spring SpEL:

The SpEL stands for Spring Expression Language. It is a powerful expression language which supports querying and manipulating an object graph at the bean creation time or run time. It is similar to other expression languages like JSP EL, OGNL, MVEL and JBoss EL etc with some additional features like method invocation and basic string templating functionality.

StandardEvaluationContext:

The StandardEvaluationContext is a class which implements EvaluationContext interface. It uses a reflection mechanism to resolve properties or methods. We can set a variable using the method setVariable on the StandardEvaluationContext. We can use this variable in the expression by using the syntax #variableName.

Example:

MulitplicationTest.java

public class MulitplicationTest {
	int num1;
	int num2;
 
	public int getNum1() {
		return num1;
	}
	public void setNum1(int num1) {
		this.num1 = num1;
	}
	public int getNum2() {
		return num2;
	}
	public void setNum2(int num2) {
		this.num2 = num2;
	}
 
	public int multiplication(){
		return num1*num2;
	}
 
}

MulitplicationTest.java

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
 
/**
 * Spring SPEL variable example.
 * @author codesjava
 */
public class Test {
 public static void main(String args[]){	
	//Create MulitplicationTest object.
	MulitplicationTest mulitplicationTest=new MulitplicationTest();
 
	//Create StandardEvaluationContext object 
	//with MulitplicationTest object.  
	StandardEvaluationContext context=
			new StandardEvaluationContext(mulitplicationTest);  
 
	//Create a parser with default settings.
	ExpressionParser parser = new SpelExpressionParser(); 
 
	//Set variables values.
	parser.parseExpression("num1").setValue(context,"10");  
	parser.parseExpression("num2").setValue(context,"20"); 
 
	//Calculate result.
	System.out.println(mulitplicationTest.multiplication());  
 }
}

Output:

200

 
Download this example.
 

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki