Advertisement



< Prev
Next >



SpEL expressions in xml configuration file



In this tutorial, we will explain we can use the Spring Expression Language(SpEL) to write expressions in xml configuration file to access classes, call methods, set the value of objects(beans) and the properties.




Creating the Java class - EmployeeAccounts


We are going to create a java class named EmployeeAccounts within the decodejava package and this class contains a String property named account_Id, int property balance and and an Employee object. Besides this, we are also going to define a couple of getter and setter method to these properties of EmployeeAccounts class.


package decodejava;

public class EmployeeAccounts 
{
	private Employee employee;
	private int balance;
	private String account_Id;

	
	public String getAccount_Id() 
	{
		return account_Id;
	}

	public void setAccount_Id(String account_Id) 
	{
		this.account_Id = account_Id;
	}

	
	
	public Employee getEmployee() 
	{
		return employee;
	}

	public void setEmployee(Employee employee) 
	{
		this.employee = employee;
	}

	
	
	public int getBalance() 
	{
		return balance;
	}
	
	public void setBalance(int balance) 
	{
		this.balance = balance;
	}

	
}





Creating a Java Class - Employee


We are going to create a Java class named Employee within the decodejava package and this class is going to contain a few properties, such as - These properties will be assigned a value by the Spring Container, when the Employee bean is created by it using the configuration xml file(to be created in the upcoming section).


package decodejava;


public class Employee 
{
	private int id;
	private String name;
	private char positionGrade;
	private float yearsOfExp;
	static String message;
	

	public void setId(int id) 
	{
		this.id = id;
	}
	
	public int getId() 
	{
		return id;
	}

	public float getYearsOfExp() 
	{
		return yearsOfExp;
	}

	public void setYearsOfExp(float yearsOfExp) 
	{
		this.yearsOfExp = yearsOfExp;
	}

	public char getPositionGrade() 
	{
		return positionGrade;
	}

	public void setPositionGrade(char positionGrade) 
	{
		this.positionGrade = positionGrade;
	}

	public String getName() 
	{
		return name;
	}
	
	public String setName(String name) 
	{
		this.name = name;
		return this.name;
	}
	
	
	
	public static String getMessage()
	{
		return message;
	}
	
	public static void setMessage(String m)
	{
		message = m;
	}
}



Advertisement




Adding the Utility class that calls the Spring API


Next, we are going to create another class named - Utility, which is a simple java class.

Utility.java
package decodejava;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Utility {

	public static void main(String[] args) throws NoSuchMethodException, SecurityException 
	{
		ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
		EmployeeAccounts employeeAcc = context.getBean("EmployeeAccountsBean", EmployeeAccounts.class);
	
		Employee emp = employeeAcc.getEmployee();
		
		System.out.println("Employee's Name :  " + emp.getName());
		System.out.println("Employee's ID : " + emp.getId());
		System.out.println("Employee's Position Grade :  " + emp.getPositionGrade());
		System.out.println("Employee's Years Of Exp : " + emp.getYearsOfExp());
		System.out.println("Message for employee : " + emp.getMessage());
	
		
		System.out.println("Employee's Balance :  " + employeeAcc.getBalance());
		System.out.println("Employee's ID : " + employeeAcc.getAccount_Id());
		
	}

}



The Utility class uses the ApplicationContext container(an interface) of Spring Framework by creating its instance using its implemented class FileSystemXmlApplicationContext, which loads the configuration xml file - config.beans.xml. The configuration file creates two beans -
  • An Employee bean and initiates it using SpEL expressions, which automatically calls the setter methods.
  • An EmployeeAccounts bean and initiates it with a value using SpEL expressions, which automatically calls the setter methods i.e. setEmployee, setAccount_Id(), and setBalance().





  • Adding a configuration file


    Next, we are going to add a configuration file to our project. This configuration document is an Extensible Markup Language(XML) file, ending with .xml extension and we are going to name file as config.beans.xml.

    In this file, we have configured a Company bean with a unique id and it contains a collection of Employee beans.


    config.beans.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:util="springframework.org/schema/util"
           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
           https://www.springframework.org/context/spring-context.xsd
           ">
               
    <bean id="EmployeeBean" class="decodejava.Employee">
    			<property name="name" value="#{'Emp1'}"></property>
    			<property name="id" value="#{1}"></property>
    			<property name="positionGrade" value="#{'A'}"></property>
    			<property name="yearsOfExp" value="#{6.5}"></property>
    			<property name="message" value="#{'Best of luck!'}"></property>
    </bean>
    			
    <bean id="EmployeeAccountsBean" class="decodejava.EmployeeAccounts">
    			<property name="employee" value="#{EmployeeBean}"></property>
    			<property name="balance" value="10000"></property>
    			<property name="account_Id" value="1001"></property>
    </bean>
    
    </beans>



    This mapping document has a parent <beans> tag as the root element and its individual child elements, each with a <bean> tag, containing all the attributes such as -


    Advertisement




    Adding JARs


    • We are going to add some JARs files to our Java project. These JARs are required in order to successfully execute a Spring project.

      All these JARs are included in the folder named libs(within our Spring installation folder). So, we need to add all the JARs in the libs folder to our build path of our Java project.

      Note : Those who don't know how to add JARs to the build path of your Java project in Eclipse IDE, please refer to our section Adding JARs to your Spring project in Eclipse.






    Directory Structure of Project




    The picture above depicts how and where to arrange classes and interfaces comprising this Spring Project, in a specific directory structure.

    Project Folder - SpELWithXML is the name of our Project and it is a top-level directory.






    Execution


    Finally, after executing Utility class, you will get the following output within the Console window. This output shown below, shows how the Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml, access the beans specified in it, instantiate the Employee class and EmployeeAccounts class, by calling their respective setter methods.


    Aug 24, 2018 5:29:14 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: startup date [Fri Aug 24 17:29:14 2018]; root of context hierarchy
    Aug 24, 2018 5:29:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [config.beans.xml]
    Employee's Name :  Emp1
    Employee's ID : 1
    Employee's Position Grade :  A
    Employee's Years Of Exp : 6.5
    Message for employee : Best of luck!
    Employee's Balance :  10000
    Employee's ID : 1001


    This concludes how to use the Spring Expression Language(SpEL) to write expressions in xml configuration file .




    Please share this article -




    < Prev
    Next >
    < SpEL with a Map
    Spring with Java Mail >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement