Spring EL method invocation example
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的更多相关文章
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- Spring学习(十三)-----Spring 表达式语言(Spring EL)
本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂 ...
- Spring Remoting: Remote Method Invocation (RMI)--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...
- Spring之RMI 远程方法调用 (Remote Method Invocation)
RMI 指的是远程方法调用 (Remote Method Invocation) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程 ...
- 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 ...
- Test Spring el with ExpressionParser
Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...
- 把功能强大的Spring EL表达式应用在.net平台
Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...
- java的RMI(Remote Method Invocation)
RMI 相关知识RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网络 ...
- Spring EL regular expression example
Spring EL supports regular expression using a simple keyword "matches", which is really aw ...
随机推荐
- Android开发之网络请求HttpURLConnection
转:http://blog.csdn.net/guolin_blog/article/details/12452307 Android中主要提供了两种方式来进行HTTP操作,HttpURLConnec ...
- Mac下安装HBase及详解
Mac下安装HBase及详解 1. 千篇一律的HBase简介 HBase是Hadoop的数据库, 而Hive数据库的管理工具, HBase具有分布式, 可扩展及面向列存储的特点(基于谷歌BigTabl ...
- POJ1037A decorative fence(好dp)
1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...
- linux 压缩/解压命令大全
.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gun ...
- HDU 1425 sort 【哈希入门】
题意:给出n个数,输出前m大的数 和上一题一样,将输入的数加上一个极大地值作为地址 #include<iostream> #include<cstdio> #include&l ...
- Sciter/HTMLayout内存占用评测
先从最基础的Exe文件的执行说起: Exe文件要在系统中执行,首先要将Exe文件本身加载入内存中,并且通常在内存中加载完成的Exe所占空间大小会比实际所占的磁盘空间大一些,这是由内存的特殊设定所决定的 ...
- Android获取SharedPreferences失败,且App无法启动
说明: 一个app访问另外一个app的SharedPreferences,程序启动之后没有显示界面就卡死了,无任何提示信息. 错误原因: 应用调用createPackageContext失败,但是也不 ...
- Java Socket(2): 异常处理
1 超时 套接字底层是基于TCP的,所以socket的超时和TCP超时是相同的.下面先讨论套接字读写缓冲区,接着讨论连接建立超时.读写超时以及JAVA套接字编程的嵌套异常捕获和一个超时例子程序的抓包示 ...
- H.264码流结构解析
from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...
- HDU 3333-Turing Tree(BIT好题)
题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间内不含重复数的和 分析: 这类题目觉得很好,很练思维,觉得不太好做. 用BIT维护和,我们可以从前向后扫一遍序列,当 ...