Pages Navigation Menu

Coding is much easier than you think

Bean Method invocation using spEL

Bean Method invocation  using spEL



Step 1 : Create the POJO

File :

package com.simpleCodeStuffs.spEL;

public class Customer {

	private String name;
	private double bill;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public double getBill() {
		return bill;

	public void setBill(double bill) {
		this.bill = bill;


File :

package com.simpleCodeStuffs.spEL;

public class Amount {

	public double billAmount() {
		return 100.9;

The scenario is such that the value of bill€™ attribute of ‘Customer’€™ depends on the value returned from ‘billAmount()’ method of ‘Amount’

Step 2 : Create the xml file


File : elBeans.xml


Note here, method invocation is done on Amount class through the bean as

spEL #{amount.billAmount()} and the returned value is set to bill using injection.


Step 3 : Create the main class

File :

package com.simpleCodeStuffs.spEL;

import org.springframework.context.ApplicationContext;

public class MainClass {

	public static void main(String[] args) {

		ApplicationContext context =
         		new ClassPathXmlApplicationContext("elBeans.xml");
		Amount bill = (Amount) context.getBean("amount");
		Customer customer = (Customer) context.getBean("cust");

		System.out.println("Customer name: " + customer.getName());
		System.out.println("Bill amount: " + customer.getBill());



Step 4 :

The output is




Leave a Comment

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