Spring Tutorial - Spring Expression Language Usage








In Spring Expression Language, we can reference a bean and its nested properties using a 'dot (.)' syntax.

bean.property_name

In the following code, we inject the value of "country" property from "addressBean" bean into "customer" class "country" property.

public class Server {
  @Value("#{addressBean.country}")
  private String country;
    ...
}




Example

The following code defines an Address bean and marks the bean with Spring Expression Language.

It fills the street with string value, fills the postcode with int value. It also defines a utility method getFullAddress to return the full address from post code, street, and country.

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("addressBean")
public class Address {

    @Value("Main Street, New York")
    private String street;

    @Value("123456")
    private int postcode;

    @Value("US")
    private String country;

    public String getFullAddress(String prefix) {
        return prefix + " : " + street + " " + postcode + " " + country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "Address [street=" + street + ", postcode=" + postcode
                + ", country=" + 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;
    }
}

The following code uses the value defined in the Address Java bean to fill the properties in the Server bean.

In Spring Expression language we can also call the method from a bean.

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("myServer")
public class Server {
    @Value("#{addressBean}")
    private Address address;
    @Value("#{addressBean.country}")
    private String country;
    @Value("#{addressBean.getFullAddress('java2s')}")
    private String fullAddress;
    @Override
    public String toString() {
        return "Server [address=" + address + "\n, country=" + country
                + "\n, fullAddress=" + fullAddress + "]";
    }
    public Address getAddress() {
      return address;
    }
    public void setAddress(Address address) {
      this.address = address;
    }
    public String getCountry() {
      return country;
    }
    public void setCountry(String country) {
      this.country = country;
    }
    public String getFullAddress() {
      return fullAddress;
    }
    public void setFullAddress(String fullAddress) {
      this.fullAddress = fullAddress;
    }
}

The following code shows how to fill the same data in xml file configuration.

<beans xmlns="http://www.springframework.org/schema/beans"
  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">
  <bean id="myServer" class="com.java2s.core.Server">
    <property name="address" value="#{addressBean}" />
    <property name="country" value="#{addressBean.country}" />
    <property name="fullAddress" value="#{addressBean.getFullAddress('java2s')}" />
  </bean>
  <bean id="addressBean" class="com.java2s.core.Address">
    <property name="street" value="Main Street, New York" />
    <property name="postcode" value="123456" />
    <property name="country" value="US" />
  </bean>
</beans>




Test

<beans xmlns="http://www.springframework.org/schema/beans"
  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">
  <bean id="myServer" class="com.java2s.core.Server">
  </bean>
</beans>

The following code shows how to run the code above.

package com.java2s.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
  public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
        Server obj = (Server) context.getBean("myServer");
      System.out.println(obj);
  }
}

Example 2

The following code shows how to call a method with no parameter in Spring Expression Language.

First, we define a Java bean with a method to return a double value.

package com.java2s.core;
import org.springframework.stereotype.Component;
@Component("priceBean")
public class Price {
  public double getSpecialPrice() {
    return new Double(99.99);
  }
}

In the following code we call the method defined above in Spring Expression Language.

  @Value("#{priceBean.getSpecialPrice()}")
  private double amount;

We can also call the 'toUpperCase()' method on the String literal.

    @Value("#{'java2s'.toUpperCase()}")
  private String name;

Full source code

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("myServer")
public class Server {

  @Value("#{'java2s'.toUpperCase()}")
  private String name;

  @Value("#{priceBean.getSpecialPrice()}")
  private double amount;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public double getAmount() {
    return amount;
  }

  public void setAmount(double amount) {
    this.amount = amount;
  }

  @Override
  public String toString() {
    return "Server [name=" + name + ", amount=" + amount + "]";
  }

}

The following code shows how to rewrite the code above in bean definition XML file.

<beans xmlns="http://www.springframework.org/schema/beans"
  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">

  <bean id="myServer" class="com.java2s.core.Server">
    <property name="name" value="#{'java2s'.toUpperCase()}" />
    <property name="amount" value="#{priceBean.getSpecialPrice()}" />
  </bean>

  <bean id="priceBean" class="com.java2s.core.Price" />

</beans>