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 ...
随机推荐
- MySQL select into 和 SQL select into
现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去,虽然可以用以下语句进行复制,总觉得不爽,希望各位帮助下我,谢谢. answer 01: create table d ...
- java中的线程创建和使用
Java中实现多线程有两种途径:继承Thread类或者实现Runnable接口.Runnable是接口,建议用接口的方式生成线程,因为接口可以实现多继承,况且Runnable只有一个run方法,很适合 ...
- Android提供的LruCache类简介
分类: Android开发 2013-02-06 15:26 26733人阅读 评论(10) 收藏 举报 package android.util; import import /** * A cac ...
- Qt之自定义界面(QMessageBox)
简述 通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget.QDialog.QMainWindow. 大多数窗体的实现都是采用控件堆积来完 ...
- Nodejs express中创建ejs项目 error install Couldn't read dependencies
最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了 书上命令为: express -t ejs microblog 可是执行 ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...
- 求强连通分量模板(tarjan算法)
关于如何求强连通分量的知识请戳 https://www.byvoid.com/blog/scc-tarjan/ void DFS(int x) { dfn[x]=lowlink[x]=++dfn_cl ...
- POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...
- 《C++ Primer 4th》读书笔记 第8章-标准IO库
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936457.html
- 【html】页面制作规范文档
每天都在写html/css/js代码,总结的一些页面制作的规范 文件命名规范 1) 文件目录.文件名称统一用小写的英文字母.数字.下划线组合,文件名要与表现的内容相近,不到万不得已不要以拼音作为名称, ...