Spring EL method invocation example

时间:2023-02-05 07:28:56

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 表达式语言&lpar;Spring EL&rpar;

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

  2. Spring学习&lpar;十三&rpar;-----Spring 表达式语言&lpar;Spring EL&rpar;

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

  3. Spring Remoting&colon; Remote Method Invocation &lpar;RMI&rpar;--转

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

  4. Spring之RMI 远程方法调用 &lpar;Remote Method Invocation&rpar;

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

  5. Error creating bean with name &&num;39&semi;sqlSessionFactory&&num;39&semi; defined in class path resource &lbrack;config&sol;spring&sol;applicationContext&period;xml&rsqb;&colon; Invocation of init method failed&semi;

    我报的错: 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表达式应用在&period;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. asp&period;net中套用母版页之后的findcontrol

    套用模板页之后,如果要在内容页中查找某个控件,需要先找到模板页中的ContentPlaceHolder,在通过ContentPlaceHolder查找代码,如下: LinkButton btn = t ...

  2. MongoDB各种查询操作详解

    这篇文章主要介绍了MongoDB各种查询操作详解,包括比较查询.关联查询.数组查询等,需要的朋友可以参考下   一.find操作 MongoDB中使用find来进行查询,通过指定find的第一个参数可 ...

  3. native2ascii 国际资源文件编码

    将common_zh_CN.properties 转换为 本地文件 common_zh_CN_src.properties native2ascii -reverse common_zh_CN.pro ...

  4. Qt——树的搜索

    一.Qt中的树 Qt中树的实现有两种方式.第一种是使用Qt提供的QTreeWidget,很多函数都封装好,比较方便:另一种是通过QTreeView实现,设置它的数据模型,比如使用QStandardIt ...

  5. Maven打包web工程成WAR

    其实不一定要通过Goals:package来打war包,直接run as maven bulid也行:

  6. &ast;&lbrack;topcoder&rsqb;TheMatrix

    http://community.topcoder.com/stat?c=problem_statement&pm=13035 矩阵棋盘的题,比较典型.可以定两条column夹住,然后上下扫, ...

  7. javascript的本地存储 cookies、localStorage

    一.cookies 本地存储 首先是常用的cookies方法,网上有很多相关的代码以及w3cSchool cookies. // 存储cookies function setCookie(name,v ...

  8. alloc &amp&semi; init &amp&semi; dealloc

    在Objective-C中,alloc,init和dealloc是经常使用的函数,那么它们内部到底是如何实现的呢?通过查看libobjc运行时库,可以发现他们的工作原理. 1 alloc alloc的 ...

  9. 什么时候需要使用Double? double、float、decimal的区别

    原文:什么时候需要使用Double? double.float.decimal的区别 float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位) double: ...

  10. ambari下的flume和kafka整合

    1.配置flume #扫描指定文件配置 agent.sources = s1 agent.channels = c1 agent.sinks = k1 agent.sources.s1.type=ex ...