In this article, I will show you how to use spring EL to invoke a method on a bean or a literal and inject the method returned value into property.
This example uses the following frameworks:
Dependencies
Add the following dependencies:
spring-core
spring-context
spring-beans
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javarticles.spring</groupId> <artifactId>springListExample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <properties> <spring.version>4.1.6.RELEASE</spring.version> </properties> </project>
Spring EL method invocation
You can invoke methods using spring EL the way you invoke methods in java. For example, to invoke a method on a bean:
"#{beanName.method()}"
In Spring EL, you can reference a bean, and nested properties using a ‘dot (.)‘ symbol.
"#{listOfTopics.getTopicsList().size()}"
Methods can also be invoked on literals, for example:
"#{'JavaThreads'.toUpperCase()}"
Spring EL in Annotation
Let’s see some examples of method invocation in spring EL. We will use some simple beans related to some topics of training.
A topic is represented by Topic
bean.
Topic:
package com.javarticles.spring; public class Topic { private String name; public Topic(){} public Topic(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return name; } }
You can create a list of topics using ListOfTopics
bean.
ListOfTopics:
package com.javarticles.spring; import java.util.ArrayList; import java.util.List; public class ListOfTopics { private String name; private List<Topic> topicsList = new ArrayList<Topic>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Topic> getTopicsList() { return topicsList; } public void setTopicsList(List<Topic> topicsList) { this.topicsList = topicsList; } public String toString() { return name + topicsList; } }
Let’s configure the above beans in spring XML context.
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.javarticles.spring" /> <bean id="listOfTopics" class="com.javarticles.spring.ListOfTopics"> <property name="topicsList"> <list> <ref local="java" /> <ref local="scala" /> </list> </property> </bean> <bean id="java" class="com.javarticles.spring.Topic"> <property name="name" value="JavaThreads" /> </bean> <bean id="scala" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaFunctions" /> </bean> </beans>
A Training
bean is a bean composed using the Topic
and ListOfTopics
beans.
We will use spring EL to populate training bean’s members.
- @Value(“#{java.getName()}”) – Invoke
getName()
method on beanjava
- @Value(“#{listOfTopics.getTopicsList().size()}”) – Invoke
getTopicsList()
on beanlistOfTopics
. The return value is a list of topics. Callsize()
on the returned list to know the size. - @Value(“#{java.getName().toUpperCase() == ‘JavaThreads’.toUpperCase()}”) – You can call methods on literals,
'JavaThreads'.toUpperCase()
. It is compared withjava.getName().toUpperCase()
, the returned value is a boolean - @Value(“#{listOfTopics.getTopicsList()[0]}”) – Returns the first topic from the list of topics
- @Value(“#{T(java.util.Arrays).asList(new com.javarticles.spring.Topic(‘Spring Data’), listOfTopics.getTopicsList()[0])}”) – Creates a custom list. We have used spring EL to make a static method call on
Arrays.asList()
. We pass in a newTopic
and an existing topic.
Training:
package com.javarticles.spring; import java.util.List; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Training { @Value("#{java.getName()}") private String javaTopic; @Value("#{listOfTopics.getTopicsList().size()}") private int totalTopics; @Value("#{java.getName().toUpperCase() == 'JavaThreads'.toUpperCase()}") private boolean hasThreadTutorial; @Value("#{listOfTopics.getTopicsList()[0]}") private Topic firstTopic; @Value("#{T(java.util.Arrays).asList(new com.javarticles.spring.Topic('Spring Data'), listOfTopics.getTopicsList()[0])}") private List<Topic> customTopics; public String getJavaTopic() { return javaTopic; } public int getTotalTopics() { return totalTopics; } public boolean isHasThreadTutorial() { return hasThreadTutorial; } public Topic getFirstTopic() { return firstTopic; } public List<Topic> getCustomTopics() { return customTopics; } public void setJavaTopic(String javaTopic) { this.javaTopic = javaTopic; } public void setTotalTopics(int totalTopics) { this.totalTopics = totalTopics; } public void setHasThreadTutorial(boolean hasThreadTutorial) { this.hasThreadTutorial = hasThreadTutorial; } public void setFirstTopic(Topic firstTopic) { this.firstTopic = firstTopic; } public void setCustomTopics(List<Topic> customTopics) { this.customTopics = customTopics; } public String toString() { return "Total topics: " + totalTopics + ", Java Topic: " + javaTopic + ", has thread tutorial: " + hasThreadTutorial + ", first topic: " + firstTopic + ", custom topics: " + customTopics; } }
To test the spring EL method invocation we just need to load the context and then print the training bean.
SpringELMethodInvocationExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringELMethodInvocationExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); try { Training training = (Training) context.getBean("training"); System.out.println(training); } finally { context.close(); } } }
Output:
Total topics: 2, Java Topic: JavaThreads, has thread tutorial: true, first topic: JavaThreads, custom topics: [Spring Data, JavaThreads]
Spring EL in spring context XML
In this example, we achieve the above using spring context XML.
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.javarticles.spring" /> <bean id="listOfTopics" class="com.javarticles.spring.ListOfTopics"> <property name="topicsList"> <list> <ref local="java" /> <ref local="scala" /> </list> </property> </bean> <bean id="java" class="com.javarticles.spring.Topic"> <property name="name" value="JavaThreads" /> </bean> <bean id="scala" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaFunctions" /> </bean> <bean id="myTraining" class="com.javarticles.spring.Training"> <property name="javaTopic" value="#{java.getName()}" /> <property name="totalTopics" value="#{listOfTopics.getTopicsList().size()}" /> <property name="hasThreadTutorial" value="#{java.getName().toUpperCase() == 'JavaThreads'.toUpperCase()}" /> <property name="firstTopic" value="#{listOfTopics.getTopicsList()[0]}" /> <property name="customTopics" value="#{T(java.util.Arrays).asList(new com.javarticles.spring.Topic('Spring Data'), listOfTopics.getTopicsList()[0])}" /> </bean> </beans>
SpringELMethodInvocationExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringELMethodInvocationExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); try { Training training = (Training) context.getBean("training"); System.out.println(training); System.out.println(context.getBean("myTraining")); } finally { context.close(); } } }
Output:
Total topics: 2, Java Topic: JavaThreads, has thread tutorial: true, first topic: JavaThreads, custom topics: [Spring Data, JavaThreads] Total topics: 2, Java Topic: JavaThreads, has thread tutorial: true, first topic: JavaThreads, custom topics: [Spring Data, JavaThreads]
Download the source code
This was an example about spring EL Method Invocation.