Share on Google+Share on Google+

Calling Static Methods Using SpEL

In this tutorial you will learn how to call Static Methods Using SpEL

Calling Static Method Using SpEL

Spring 3 provides powerful Expression Language which can be used to wire values into beans properties by calling static method of any bean using SpEL's T() operator.

Lets take an example to demonstrate how SpEL is used to wire value by calling static  method. The MyClass class contains property named "random" which is of String type.

package spel.callingstaticmethodsusingSpEL;

public class MyClass
private String random;

public void setRandom(String random) {
this.random = random;
public String getRandom() {
return random;

spring-beans.xml: We will see how to use static method random() of java.util.Math class to wire random property of the MyClass class using T() operator of Spring expression Language. This operator takes fully qualified class name.

<beans xmlns=""

<bean id="myClass" class="spel.callingstaticmethodsusingSpEL.MyClass">
<property name="random" value="#{T(java.lang.Math).random()}"/>

</beans> This class loads the context configuration file spring-beans-list.xml from the class path.

package spel.callingstaticmethodsusingSpEL;

import org.springframework.context.ApplicationContext;

import spel.callingstaticmethodsusingSpEL.MyClass;

public class AppMain{
public static void main( String[] args ){
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"spel\\callingstaticmethodsusingSpEL\\spring-beans-list.xml"});
MyClass myObj = (MyClass)appContext.getBean("myClass");

System.out.println("Random Value: "+myObj.getRandom());

The output of the above program will be as below:

Random Value: 0.6188206590561351

Download Select Source Code


Posted on: April 7, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null