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>
<!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-core/5.3.23/spring-core-5.3.23.jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-beans/5.3.23/spring-beans-5.3.23.jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.23</version>
</dependency>
<!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-context-support/5.3.23/spring-context-support-5.3.23.jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023