在Spring EL,可以使用点(.)符号嵌套属性参考一个bean。例如,“bean.property_name”。
public class Customer {

	@Value("#{addressBean.country}")
private String country;
在上面的代码片段,它从“addressBean” bean注入了“country”属性到现在的“customer”类的“country”属性的值。

Spring EL以注解的形式

请参阅下面的例子,演示如何使用使用 SpEL 引用一个bean,bean属性也它的方法。
package com.yiibai.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('yiibai')}")
private String fullAddress; //getter and setter methods @Override
public String toString() {
return "Customer [address=" + address + "\n, country=" + country
+ "\n, fullAddress=" + fullAddress + "]";
} }
package com.yiibai.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("addressBean")
public class Address { @Value("GaoDeng, QiongShang")
private String street; @Value("571100")
private int postcode; @Value("CN")
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 + "]";
} }

执行结果

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

输出结果

Customer [address=Address [street=GaoDeng, QiongShang, postcode=571100, country=CN]
, country=CN
, fullAddress=yiibai : GaoDeng, QiongShang 571100 CN]

Spring EL以XML的形式

请参阅在XML文件定义bean的等效版本。
<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.yiibai.core.Customer">
<property name="address" value="#{addressBean}" />
<property name="country" value="#{addressBean.country}" />
<property name="fullAddress" value="#{addressBean.getFullAddress('yiibai')}" />
</bean> <bean id="addressBean" class="com.yiibai.core.Address">
<property name="street" value="GaoDeng, QiongShang" />
<property name="postcode" value="571100" />
<property name="country" value="CN" />
</bean> </beans>

Spring EL bean引用实例的更多相关文章

  1. Spring EL方法调用实例

    Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用SpEL方法调用”. Spring EL在注解的形式 了解如何实现Spring EL方法 ...

  2. Spring EL hello world实例

    Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行.此外,所有的Spring表达式都可以通过XML或注解. 在本教程中,我们将学习如何使用Spring表达式语言(SpEL) ...

  3. Spring框架bean的配置(2):SpEL:引用 Bean、属性和方法。。。

    将这些架包放入在工程目录下建立的lib文件夹里,并解压 commons-logging-1.1.1 spring-aop-4.0.0.RELEASE spring-beans-4.0.0.RELEAS ...

  4. 【spring bean】 spring中bean之间的引用以及内部bean

    在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1>  <ref  bean="另 ...

  5. spring listener引用spring中bean

    在SSH项目开发中,会使用到监听器Listener,并且有时需要在监听器中完成数据库的操作等动作,此时需要在Listener中使用到Spring容器中的Bean.Spring容器本身就是在web.xm ...

  6. Spring源码分析(十四)从bean的实例中获取对象

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在getBean方法中,getObjectForBeanlnstance ...

  7. Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子

    在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...

  8. Spring EL运算符实例

    Spring EL支持大多数标准的数学,逻辑和关系运算符. 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , l ...

  9. Spring内部bean实例

    在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean.内部bean支持setter注入“property”和构造器注入"constructor-arg“. ...

随机推荐

  1. 【转】Python OCR识别图片验证码

    转载自:博客 对于某些网站登录的时候,往往需要输入验证码才能实现登录.如果要爬虫这类网站,往往总会比这个验证码导致无法爬取数据.以下介绍一种比较折中的方法,也是比较可行的方法: 实现思想: 1.通过截 ...

  2. wordpress后台加载速度异常缓慢排查记录(原创)

    原因在于在function.php函数中加入了下面的代码导致了缓慢: //停用版本更新通知remove_action('load-update-core.php', 'wp_update_themes ...

  3. ubuntu访问win10下的磁盘

    在ubuntu下访问win10的磁盘时,提示出错. 先用命令查看一下磁盘挂载情况. sudo fdisk -l 会看到一些信息 我要访问的是E盘,也就是 /dev/sda6 这个分区 使用命令ntfx ...

  4. docker-compose RabbitMQ与Nodejs接收端同时运行时的错误

    首先讲一下背景: 我现在在开发的一个项目,需要运行RabbitMQ和Nodejs接收端(amqplib库),但是在Nodejs接收端运行时,无法连接至RabbitMQ端,经常提示说 connect E ...

  5. ftp缓存信息

    using System.Collections.Generic; using NewTempo.Ftp; using System.IO; using NshowAdClient.NshowAdSe ...

  6. HTTP协议的重新学习

    思论:做互联网一年多了,想了想对http协议的认识还处于很笼统的阶段,抽休息时间,重新梳理一下自己的网络知识. 1.什么叫HTTP协议? HTTP协议是Hyper TEXT Transfer Prot ...

  7. NOIP2013 D1 T2火柴排队

    题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...

  8. 洛谷——P1349 广义斐波那契数列

    题目描述 广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列.今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an除以m的余数. 输入输出格 ...

  9. Python开发基础-Day31 Event对象、队列和多进程基础

    Event对象 用于线程间通信,即程序中的其一个线程需要通过判断某个线程的状态来确定自己下一步的操作,就用到了event对象 event对象默认为假(Flase),即遇到event对象在等待就阻塞线程 ...

  10. 基础数据类型汇总补充;集合set ;深浅copy

    首先回顾: 小数据池:int -5~256str 特殊字符,*数字20 ascii : 8位 1字节 表示1个字符unicode 32位 4个字节 表示一个字符utf- 8 1个英文 8位,1个字节 ...