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

In this example 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="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="myBean" class="org.kodejava.spring.core.el.MyOtherBean">
        <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.spring.core.el.MyOtherBean).BEAN_NAME}" />
    </bean>

</beans>

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

package org.kodejava.spring.core.el;

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

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

    public String getRandomNumber() {
        return randomNumber;
    }

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

    public String getPi() {
        return pi;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package org.kodejava.spring.core.el;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpELStaticDemo {
    public static void main(String[] args) {
        try (ClassPathXmlApplicationContext context =
                     new ClassPathXmlApplicationContext("spring-spel-static.xml")) {
            MyOtherBean bean = (MyOtherBean) 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:

bean.getRandomNumber() = 0.7173165965231882
bean.getPi()           = 3.141592653589793
bean.getName()         = MyOtherBean

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.23</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central

How do I call static method using Spring EL?

In this example you will learn how to call static method using Spring EL. The T() operator of the Spring EL can be used to call static method. First, create the following class, NumberGenerator. This class has a single property randomNumber and getter / setter method.

package org.kodejava.spring.core.el;

public class NumberGenerator {
    private int randomNumber;

    public int getRandomNumber() {
        return randomNumber;
    }

    public void setRandomNumber(int randomNumber) {
        this.randomNumber = randomNumber;
    }
}

Now, create the following spring configuration file and save it in a file called SpELStatic.xml. In this configuration we register a bean called bean of type NumberGenerator. We set its randomNumber property using the value produced by the java.lang.Math.random() static method. For calling a static method we use the Spring EL T() operator, for example #{T(java.lang.Math).random()}.

<?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="bean" class="org.kodejava.spring.core.el.NumberGenerator">
        <property name="randomNumber" value="#{T(java.lang.Math).random() * 100 + 1}" />
    </bean>

</beans>

The program below load the spring configuration and get the NumberGenerator bean to create a random number.

package org.kodejava.spring.core.el;

import org.springframework.context.support.ClassPathXmlApplicationContext;

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

            NumberGenerator number = (NumberGenerator) context.getBean("bean");
            System.out.println("Random number: " + number.getRandomNumber());
        }
    }
}

And example result you might get when running the program:

Random number: 33

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.23</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central