How do I access static methods or constants in Spring EL?

In this examples you will learn how to access class-scoped methods or constants using Spring Expression Language. To access a class-scoped methods or constants you will need to use the T() operator of the Spring EL, for example T(java.lang.Math). This operator will give us the access to static methods and constants on a given class. As an example we can access the Math.PI in Spring EL like T(java.lang.Math).PI.

Just like accessing the static constants we can also access a static method in the same way. For example we can call the Math.random() method in Spring EL like this T(java.lang.Math).random().

Now let’s see how we do these inside a spring configuration file. In this configuration we create a bean called MyBean that have properties such as randomNumber, pi and name.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="myBean" class="org.kodejava.example.spring.model.MyBean">
        <property name="randomNumber" value="#{T(java.lang.Math).random()}"/>
        <property name="pi" value="#{T(java.lang.Math).PI}"/>
        <property name="name" value="#{T(org.kodejava.example.spring.model.MyBean).BEAN_NAME}"/>

Here are out bean class and an application class that run the spring configuration for the demo.

package org.kodejava.example.spring.model;

public class MyBean {
    public static final String BEAN_NAME = "MyBean";

    private String randomNumber;
    private String pi;
    private String name;

    public void setRandomNumber(String randomNumber) {
        this.randomNumber = randomNumber;

    public String getRandomNumber() {
        return randomNumber;

    public void setPi(String pi) {
        this.pi = pi;

    public String getPi() {
        return pi;

    public void setName(String name) { = name;

    public String getName() {
        return name;
package org.kodejava.example.spring.spel;

import org.kodejava.example.spring.model.MyBean;
import org.springframework.context.ApplicationContext;

public class SpELStaticDemo {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spel-static.xml");

        MyBean bean = (MyBean) context.getBean("myBean");
        System.out.println("bean.getRandomNumber() = " + bean.getRandomNumber());
        System.out.println("bean.getPi()           = " + bean.getPi());
        System.out.println("bean.getName()         = " + bean.getName());

When executing the program you will get the following result as the output:

Feb 21, 2014 1:37:30 PM prepareRefresh
INFO: Refreshing[email protected]: startup date [Fri Feb 21 13:37:30 CIT 2014]; root of context hierarchy
Feb 21, 2014 1:37:30 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spel-static.xml]
bean.getRandomNumber() = 0.7644247352874567
bean.getPi()           = 3.141592653589793
bean.getName()         = MyBean



Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

Leave a Reply