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 ...
随机推荐
- $.toJSON的用法或把数组转换成json类型
1. html页面全部代码 <html> <head> <title></title> <script src="../../S ...
- 对List顺序,逆序,随机排列实例代码
ackage Test; import java.util.Collections; import java.util.LinkedList; import java.util.List; p ...
- JAVA中获取工程路径的方法
在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...
- ACM - ICPC World Finals 2013 A Self-Assembly
原题下载 : http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这道题其实是2013年我AC的第一道题,非常的开心,这 ...
- Web Api 返回参数,实现统一标准化!
string camelCaseObj = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.None, new JsonSer ...
- datatable 的ajax修改参数,post可以传参处理
datatables常用参数记录 { "searchable": false, "orderabl ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- C++实现String
# include <iostream> # include <memory> # include <cstring> using namespace std; c ...
- android view的setVisibility方法值的意思
android view的setVisibility方法值的意思 有三个值 visibility One of VISIBLE, INVISIBLE, or GONE. 常量值为0,意思是可见的 常 ...
- Ant编译和部署java web项目
1.在myeclipse中创建javaWeb项目AntDemo 2.将build.xml放到AntDemo根目录下 3.修改build.xml中的Project name,工程目录名,工程名,还有to ...