Spring 专题
专题目录
您的位置:java > Spring专题 > Spring EL方法调用实例
Spring EL方法调用实例
作者:--    发布时间:2019-11-20
spring表达式语言(使用spel)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用spel方法调用”。

spring el在注解的形式

了解如何实现spring el方法调用与@value注释。
package com.h3.core;

import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;

@component("customerbean")
public class customer {

	@value("#{'h3'.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.h3.core;

import org.springframework.stereotype.component;

@component("pricebean")
public class price {

	public double getspecialprice() {
		return new double(199.09);
	}

}

输出

customer [name=h3, amount=199.09]

一点解释

在字串文本上调用 touppercase()方法。
@value("#{'h3'.touppercase()}")
private string name;

在 ‘pricebean‘ bean上调用getspecialprice() 方法

@value("#{pricebean.getspecialprice()}")
private double amount;

spring el在xml的形式

请参阅在xml文件定义bean的等效版本。
<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="customerbean" class="com.h3.core.customer">
		<property name="name" value="#{'h3'.touppercase()}" />
		<property name="amount" value="#{pricebean.getspecialprice()}" />
	</bean>
 
	<bean id="pricebean" class="com.h3.core.price" />
	
</beans>

输出

customer [name=h3, amount=199.09]
下载代码 –  http://pan.baidu.com/s/1mhdwodu

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册