Spring spel bean reference tutorial

Spring SpEL:

The SpEL stands for Spring Expression Language. It is a powerful expression language which supports querying and manipulating an object graph at the bean creation time or run time. It is similar to other expression languages like JSP EL, OGNL, MVEL and JBoss EL etc with some additional features like method invocation and basic string templating functionality.

Spring SpEL Bean Reference:

Spring spel provides the facility to refer a bean and nested properties using a dot (.) symbol.

Syntax:

#{beanName.propertyName}

Example:

Address.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("addressBean")
public class Address {
	@Value("Sec-A")
	private String street;
	@Value("122001")
	private int postcode;
	@Value("India")
	private String country;
 
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public int getPostcode() {
		return postcode;
	}
	public void setPostcode(int postcode) {
		this.postcode = postcode;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
}

Student.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("studentBean")
public class Student {
	@Value("Jai")
	private String name;	
 
	@Value("#{addressBean.street}")
	private String street;
 
	@Value("#{addressBean.country}")
	private String country;
 
	@Value("#{addressBean.postcode}")
	private String postcode;
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
 
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
 
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
 
	public String getPostcode() {
		return postcode;
	}
	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}	
 
}

applicationContext.xml

<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-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 <context:component-scan base-package="com.javawithease.business" />
 
</beans>

Test.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * Spring SPEL bean reference example.
 * @author javawithease
 */
public class Test {
   public static void main(String args[]){	
	 //Get application context object.
	 ApplicationContext context = 
	   new ClassPathXmlApplicationContext("applicationContext.xml");
 
	 //Get studentBean.
	 Student student = (Student) context.getBean("studentBean");
 
	 //Print student properties.
	 System.out.println("Name: "+student.getName()); 
	 System.out.println("Street: "+student.getStreet()); 
	 System.out.println("Postal: "+student.getPostcode()); 
  	 System.out.println("Country: "+student.getCountry()); 
   }
}

Output:

Name: Jai
Street: Sec-A
Postal: 122001
Country: India

 
Download this example.