Пример вызова метода Spring EL
Язык выражений Spring (SpEL) позволяет разработчику использовать выражение для выполнения метода и вставки возвращаемого значения метода в свойство, или так называемый «SpEL method invocation».
Spring EL в аннотации
Посмотрите, как выполнить вызов метода Spring EL с аннотацией@Value.
package com.example.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("customerBean")
public class Customer {
@Value("#{'example'.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 "Customer [name=" + name + ", amount=" + amount + "]";
}
}
package com.example.core;
import org.springframework.stereotype.Component;
@Component("priceBean")
public class Price {
public double getSpecialPrice() {
return new Double(99.99);
}
}
Выход
Customer [name=MKYONG, amount=99.99]
объяснение
Вызовите метод «toUpperCase()» для литералаstring.
@Value("#{'example'.toUpperCase()}")
private String name;
Вызовите метод «getSpecialPrice()» для bean-компонента «priceBean».
@Value("#{priceBean.getSpecialPrice()}")
private double amount;
Spring EL в XML
Это эквивалентная версия в XML-файле определения компонента.
Выход
Customer [name=MKYONG, amount=99.99]
Скачать исходный код
Скачать -Spring3-EL-Method-Invocation-Example.zip (6 КБ)