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“. ...
随机推荐
- Python如何实现文本转语音
准备 我测试使用的Python版本为2.7.10,如果你的版本是Python3.5的话,这里就不太适合了. 使用Speech API 原理 我们的想法是借助微软的语音接口,所以我们肯定是要进行调用 相 ...
- [ python ] 变量及基础的数据类型
python2 和 python3 不同的编码方式 python2 默认编码方式是 ascii码 python3 默认编码方式是 utf-8 具体表现为:当 python3 和 python2 在打印 ...
- C语言使用数学库编译不通过问题
#include <stdio.h>#include <math.h> int main(){ double a = 10.0,b = 3.0; f ...
- javaScript传递参数,参数变化问题
值传递 var a=10; b(a); function b(v){ v--; } alert(a); //out 10 对象传递 var a={}; a.v=10; b(a); function b ...
- JS模块化规范CMD之SeaJS
1. 在接触规范之前,我们用模块化来封装代码大多为如下: ;(function (形参模块名, 依赖项, 依赖项) { // 通过 形参模块名 修改模块 window.模块名 = 形参模块名 })(w ...
- 十五oracle 触发器
一.触发器简介 触发器的定义就是说某个条件成立的时候,触发器里面所定义的语句就会被自动的执行.因此触发器不需要人为的去调用,也不能调用.然后,触发器的触发条件其实在你定义的时候就已经设定好了.这里面需 ...
- yum软件搜索
安装软件却记不清包名,搜索出所有带SDL的包yum list *SDL* yum install xxx 列出所有的安装套件yum group list yum group install xxx
- 用php写的一个猜数字的程序
写一个php的猜数字的小程序,提高自己对php的熟悉程度. <?php $count=rand(0,100); $number = (isset($_POST['number'])) ? $_P ...
- Redux学习之我对于其工作流程的理解和实践
目录 1 工作流程图 2 各部位职责 3 Demo 1 工作流程图 2 各部位职责 我在理解这个流程图的时候,采用的是一种容易记住的办法,并且贴切实际工作职责. 我们可以把整个Redux工 ...
- 工厂bean和bean工厂
FactoryBean(工厂bean):是bean的加工工厂,是对已知Bean的加工,是一个接口,要实现三个方法: ① Object getObject()可以对bean进行加工添加功能. ② Cla ...