Spring EL bean引用实例
public class Customer {
@Value("#{addressBean.country}")
private String country;
Spring EL以注解的形式
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的形式
<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引用实例的更多相关文章
- Spring EL方法调用实例
Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用SpEL方法调用”. Spring EL在注解的形式 了解如何实现Spring EL方法 ...
- Spring EL hello world实例
Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行.此外,所有的Spring表达式都可以通过XML或注解. 在本教程中,我们将学习如何使用Spring表达式语言(SpEL) ...
- Spring框架bean的配置(2):SpEL:引用 Bean、属性和方法。。。
将这些架包放入在工程目录下建立的lib文件夹里,并解压 commons-logging-1.1.1 spring-aop-4.0.0.RELEASE spring-beans-4.0.0.RELEAS ...
- 【spring bean】 spring中bean之间的引用以及内部bean
在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1> <ref bean="另 ...
- spring listener引用spring中bean
在SSH项目开发中,会使用到监听器Listener,并且有时需要在监听器中完成数据库的操作等动作,此时需要在Listener中使用到Spring容器中的Bean.Spring容器本身就是在web.xm ...
- Spring源码分析(十四)从bean的实例中获取对象
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在getBean方法中,getObjectForBeanlnstance ...
- Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子
在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...
- Spring EL运算符实例
Spring EL支持大多数标准的数学,逻辑和关系运算符. 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , l ...
- Spring内部bean实例
在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean.内部bean支持setter注入“property”和构造器注入"constructor-arg“. ...
随机推荐
- ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤
一.IIS 配置 启用 Web 服务器 (IIS) 角色并建立角色服务. 1.Windows Ddesktop 桌面操作系统(win7及更高版本) 导航到“控制面板” > “程序” > “ ...
- Codeforces 918C The Monster(括号匹配+思维)
题目链接:http://codeforces.com/contest/918/problem/C 题目大意:给你一串字符串,其中有'('.')'.'?'三种字符'?'可以当成'('或者')'来用,问该 ...
- Codeforces 813B The Golden Age(数学+枚举)
题目大意:如果一个数t=x^a+y^b(a,b都是大于等于0的整数)那就是一个unlucky数字.给你x,y,l,r(2 ≤ x, y ≤ 10^18, 1 ≤ l ≤ r ≤ 10^18),求出l到 ...
- 20165301陈潭飞2017-2018-2 20165301 实验三《Java面向对象程序设计》实验报告
2017-2018-2 20165301 实验三<Java面向对象程序设计>实验报告 一.敏捷开发与XP实践-1 实验要求: 在IDEA中使用工具(Code->Reformate C ...
- MySQL之查漏补缺
1.TRUNCATE语句和DELETE语句的区别 1.delete语句,是DML语句,truncate语句通常被认为是DDL语句. 2.delete语句,后面可以跟where子句,通常指定where子 ...
- C++实践积累
C++ STL vector 如何彻底清空一个vector? 实践证明,vector.clear()并不能把vector容量清空,只会让vector.size()变为零,依然很占内存.那如何让vect ...
- 全栈Python 必备库
强大的库: 转自:微信公众号 Python最棒的地方之一,就是大量的第三方库,覆盖之广,令人惊叹.Python 库有一个缺陷就是默认会进行全局安装.为了使每个项目都有一个独立的环境,需要使用工具vir ...
- rest api load test
1. SoapUI + LoadUI 2. https://github.com/jeffbski/bench-rest 3. JMeter
- TeX Live & TeXstudio 安装手记
数据库课上又看到了那位用 beamer 做 slides 的师兄,想到自己一拖再拖的LaTeX入门,决定赶快动手装个环境再说~在经过一番搜索和研究之后决定先在 windows 底下试用,选择 TeX ...
- 《构建高性能 Web站点》笔记
书名:构建高性能Web站点 出版社: 电子工业出版社 ISBN:9787121170935 一 绪论 等待的时间: (1) 数据在网络上的传输时间 (2) 站点服务器处理请求并生成回应数据的时间 ( ...