JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How to use Spring EL in XML configurations?

October 28, 2013 by Krishna Srinivasan Leave a Comment

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

[code lang=”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;
}
}
[/code]

Company.java

[code lang=”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;
}
}
[/code]

SpringExpressionDemo.java

[code lang=”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());
}
}
[/code]

beans.xml

[code lang=”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>
[/code]

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

[code]
Company Name : JavaBeat
Employee Name : Name 1
Employee ID : 0001
[/code]

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

Filed Under: Spring Framework Tagged With: Spring Framework

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved