java 方法调用表达式_使用SpringEL表达式进行方法调用

原文链接:http://www.yiidian.com/spring/spring-el-method.html

SpEL允许开发者用El运行方法函数,并且允许将方法返回值注入到属性中。

一、编写Bean类

TestMethod类

package com.yiidian;

/**

*

* @author http://www.yiidian.com

*

*/

public class TestMethod {

public Double getPrice(){

return 199.99D;

}

}

Customer类:

package com.yiidian.domain;

import java.io.Serializable;

/**

*

* @author http://www.yiidian.com

*

*/

public class Customer implements Serializable{

private String name;

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 + "]";

}

}

二、配置applicationContext.xml

xmlns:p="http://www.springframework.org/schema/p"

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.xsd">

注意:

#{'yiidian'.toUpperCase()}: 这句代表调用字符串toUpperCase()方法

#{testMethod.getPrice()}: 这句代表调用TestMehod对象的getPrice()方法

三、编写测试类

package com.yiidian.test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiidian.domain.Customer;

/**

* @author http://www.yiidian.com

*

*/

public class Demo1 {

@Test

public void test1() {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Customer customer = (Customer)context.getBean("customer");

System.out.println(customer);

}

}

四、运行结果

055f48be86453eeccb223d0a3d2a478c.png

源码下载:http://pan.baidu.com/s/1bp31pSV

欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。

如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值