How to use Spring EL in XML configurations?

In our previous tutorial we have explained in detail about how to use the spring expression language. This tutorial is a simple example to demonstrate how to use the expression language in XML configuration file.

Employee.java

package javabeat.net.core;

import org.springframework.stereotype.Component;

@Component
public class Employee {
	private String name;
	private String empId;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmpId() {
		return empId;
	}
	public void setEmpId(String empId) {
		this.empId = empId;
	}
}

Company.java

package javabeat.net.core;

import org.springframework.stereotype.Component;

@Component
public class Company {
	private String name;
	private Employee employee;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
}

SpringExpressionDemo.java

package javabeat.net.core;

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

public class SpringExpressionDemo {
	public static void main(String args[]){
		ApplicationContext context = new ClassPathXmlApplicationContext("javabeat/net/core/beans.xml");
		Company company = (Company)context.getBean("company");
		System.out.println("Company Name : " + company.getName());
		System.out.println("Employee Name : " + company.getEmployee().getName());
		System.out.println("Employee ID : " + company.getEmployee().getEmpId());
	}
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.0.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="employee" class="javabeat.net.core.Employee">
		<property name="name" value="Name 1"/>
		<property name="empId" value="0001"/>
	</bean>
  	<bean id="company" class="javabeat.net.core.Company">
  		<property name="name" value="JavaBeat"/>
  		<property name="employee" value="#{employee}"/>
  	</bean>
</beans>

If you run the above example program, you will get the output as:

Company Name : JavaBeat
Employee Name : Name 1
Employee ID : 0001

I hope this tutorial have helped you to understand how to use expression language in the spring xml configuration files.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!