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 CentralMaven CentralMaven Central

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.