Share on Google+Share on Google+

Calling Methods Using SpEL

Advertisement
In this tutorial you will learn that how to call Methods Using SpEL

Calling Methods Using SpEL

Spring 3 provides powerful Expression Language which can be used to wire values into beans properties by calling method of any bean.

Lets take an example to demonstrate how SpEL is used to wire value by calling methods.

Person.java: The Person class contains property named "name" which is of String type.

package spel.callingmethodsusingSpEL;

import java.util.*;
import spel.callingmethodsusingSpEL.PersonName;

public class Person
{
private String name;

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

PersonName.java: This class contains the firstName and lastName property to set first name and last name of the person. This class also provides a method getPersonName() which can be used to get the full name of the person.

package spel.callingmethodsusingSpEL;

import java.util.*;

public class PersonName
{
private String firstName;
private String lastName;

public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}

public String getPersonName() {
return firstName+" "+lastName;
}
}

spring-beans.xml: We will see how to use getPersonName() method to wire name property of the Person class using Spring expression Language.

<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-2.5.xsd">

<bean id="person" class="spel.callingmethodsusingSpEL.Person">
<property name="name" value="#{personName.getPersonName()}"/>
</bean>
<bean id="personName" class="spel.callingmethodsusingSpEL.PersonName">
<property name="firstName" value="Deepak"/>
<property name="lastName" value="Kumar"/>
</bean>
</beans>

AppMain.java: This class loads the context configuration file spring-beans-list.xml from the class path.

package spel.callingmethodsusingSpEL;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spel.callingmethodsusingSpEL.Person;
import java.util.*;

public class AppMain{
public static void main( String[] args ){
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"spel\\callingmethodsusingSpEL\\spring-beans-list.xml"});
Person person = (Person)appContext.getBean("person");

System.out.println("-------------------------------------");
System.out.println("Full Name of the person: "+person.getName());
System.out.println("-------------------------------------");
}
}

The output of the above program will be as below:

-------------------------------------
Full Name of the person: Deepak Kumar
-------------------------------------

Download Select Source Code

Advertisements

Posted on: April 7, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.