In Spring EL, you can reference a bean, and nested properties using a ‘dot (.)‘ symbol. For example, “bean.property_name“.

public class Customer {

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

In above code snippet, it inject the value of “country” property from “addressBean” bean into current “customer” class, “country” property.

Spring EL in Annotation

See following example, show you how to use SpEL to reference a bean, bean property and also it’s method.

package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("customerBean")
public class Customer { @Value("#{addressBean}")
private Address address; @Value("#{addressBean.country}")
private String country; @Value("#{addressBean.getFullAddress('mkyong')}")
private String fullAddress; //getter and setter methods @Override
public String toString() {
return "Customer [address=" + address + "\n, country=" + country
+ "\n, fullAddress=" + fullAddress + "]";
} }
package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("addressBean")
public class Address { @Value("Block ABC, LakeView")
private String street; @Value("98700")
private int postcode; @Value("US")
private String country; public String getFullAddress(String prefix) { return prefix + " : " + street + " " + postcode + " " + country;
} //getter and setter methods public void setCountry(String country) {
this.country = country;
} @Override
public String toString() {
return "Address [street=" + street + ", postcode=" + postcode
+ ", country=" + country + "]";
} }

连私有属性都可以直接访问

Run it

       Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);

Output

Customer [address=Address [street=Block ABC, LakeView, postcode=98700, country=US]
, country=US
, fullAddress=mkyong : Block ABC, LakeView 98700 US]

Spring EL in XML

See equivalent version 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="customerBean" class="com.mkyong.core.Customer">
<property name="address" value="#{addressBean}" />
<property name="country" value="#{addressBean.country}" />
<property name="fullAddress" value="#{addressBean.getFullAddress('mkyong')}" />
</bean> <bean id="addressBean" class="com.mkyong.core.Address">
<property name="street" value="Block ABC, LakeView" />
<property name="postcode" value="98700" />
<property name="country" value="US" />
</bean> </beans>

Spring EL method invocation example的更多相关文章

  1. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  2. Spring学习(十三)-----Spring 表达式语言(Spring EL)

    本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂 ...

  3. Spring Remoting: Remote Method Invocation (RMI)--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...

  4. Spring之RMI 远程方法调用 (Remote Method Invocation)

    RMI 指的是远程方法调用 (Remote Method Invocation) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程 ...

  5. Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/spring/applicationContext.xml]: Invocation of init method failed;

    我报的错: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSes ...

  6. Test Spring el with ExpressionParser

    Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...

  7. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  8. java的RMI(Remote Method Invocation)

    RMI 相关知识RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网络 ...

  9. Spring EL regular expression example

    Spring EL supports regular expression using a simple keyword "matches", which is really aw ...

随机推荐

  1. JAVA操作Excel 可配置,动态 生成复杂表头 复杂的中国式报表表头

    转载:开源社区http://www.oschina.net/code/snippet_1424099_49530?p=2代码] [Java]代码 该代码实现了Excel复杂表头的生成 基于sql se ...

  2. Codeforces Beta Round #2B(dp+数学)

    贡献了一列WA.. 数学很神奇啊 这个题的关键是怎么才能算尾0的个数 只能相乘 可以想一下所有一位数相乘 除0之外,只有2和5相乘才能得到0 当然那些本身带0的多位数 里面肯定含有多少尾0 就含有多少 ...

  3. bzoj2792

    首先想到二分答案是吧,设为lim 这道题难在判定,我们先不管将一个数变为0的条件 先使序列满足相邻差<=lim,这个正着扫一遍反着扫一遍即可 然后我们就要处理将一个数变为0的修改代价 当i变为0 ...

  4. UVa 10003 (可用四边形不等式优化) Cutting Sticks

    题意: 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用. 分析: d(i, j)表示切割第i个切点到第j个切点这段所需的最小费用.则有d(i, j) = ...

  5. 最受Web前端开发者欢迎的五大开发工具

    工其事,必利于器.好的开发工具毋容置疑会帮助Web前端开发者事半功倍,51CTO在上期主办的技术沙龙<大型网站PHP开发之道> 对现场的百余位Web开发者做了问卷调查,后经51CTO调研小 ...

  6. 任E行M1端口比特率特征码

    分辨率:800x480gps端口:com1比特率:4800设备特征码:01D1D008内存:128M

  7. ZigZag Conversion1

    问题描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  8. apache开源项目--dbutils

    原则上这不能说是一个持久层框架,它提供了一些Jdbc的操作封装来简化数据查询和记录读取操作.本站就是采用这个项目来读写数据库,代码非常简洁,如果你厌烦了Hibernate的庞大,不妨可以试试——DbU ...

  9. uva 11768

    // 扩展欧几里得算法 // 先求出一个解 再求出区间 [x1,x2]有几个整数符合条件// 需要注意的是 水平和垂直2种情况的处理 还有正数和负数取整的细微差别#include <iostre ...

  10. Android自定义的webView——可实现的网页文本的复制

    package com.example.customlinearlayout.view; import android.app.ProgressDialog; import android.conte ...