Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行。此外,所有的Spring表达式都可以通过XML或注解。
在本教程中,我们将学习如何使用Spring表达式语言(SpEL),注入字符串,整数,Bean到属性,无论是在XML和注释。

1. Spring Beans

两个简单Bean,后来利用 SpEL 注入值到属性,在 XML 和 注释。
package com.yiibai.core;

public class Customer {

	private Item item;

	private String itemName;

}
package com.yiibai.core;

public class Item {

	private String name;

	private int qty;

}

3. Spring EL以XML形式

使用 SpEL关闭的#{ SpEL expression }括号,请参阅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="itemBean" class="com.yiibai.core.Item">
<property name="name" value="itemA" />
<property name="qty" value="10" />
</bean> <bean id="customerBean" class="com.yiibai.core.Customer">
<property name="item" value="#{itemBean}" />
<property name="itemName" value="#{itemBean.name}" />
</bean> </beans>
  1. #{itemBean} – 注入“itemBean”到“customerBean”Bean 的“item”属性。
  2. #{itemBean.name} – 注入“itemBean”的“name”属性到 “customerBean" bean的"itemname”属性。

4. Spring EL以注解形式

请参阅等效版本注释模式。

要在注解使用使用SpEL,必须通过注解注册您的组件。如果注册bean在XML和Java类中定义@Value,该@Value将无法执行。
package com.yiibai.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("customerBean")
public class Customer { @Value("#{itemBean}")
private Item item; @Value("#{itemBean.name}")
private String itemName; //... }
package com.yiibai.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("itemBean")
public class Item { @Value("itemA") //inject String directly
private String name; @Value("10") //inject interger directly
private int qty; public String getName() {
return name;
} //...
}
启用自动组件扫描。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.yiibai.core" /> </beans>
在注解模式下,可以使用@Value定义Spring EL。在这种情况下,一个String和Integer值直接注入到“itemBean”,之后又注入“itemBean”到“customerBean”属性。

5. 执行输出

运行它,无论是使用 SpEL在XML 还是注释都显示了同样的结果:
package com.yiibai.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);
}
}

输出结果

Customer [item=Item [name=itemA, qty=10], itemName=itemA]

Spring EL hello world实例的更多相关文章

  1. Spring EL方法调用实例

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

  2. Spring EL bean引用实例

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

  3. Spring EL运算符实例

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

  4. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

  5. Spring EL表达式和资源调用

    Spring EL表达式     Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于在jsp的EL表达式语言.     Spring 开发中经常涉及调用各种资源的情况, ...

  6. Spring3系列6 - Spring 表达式语言(Spring EL)

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

  7. activiti自定义流程之Spring整合activiti-modeler5.16实例(九):历史任务查询

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  8. activiti自定义流程之Spring整合activiti-modeler5.16实例(八):完成个人任务

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  9. activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

随机推荐

  1. Eclipse java项目转换为web项目

    1.打开.project文件,并修改文件, 修改如下: 找到:<natures> </natures>代码段,在代码段中加入如下内容并保存: <nature>org ...

  2. php7.33 configure

    To assign environment variables (e.g., CC, CFLAGS...), specify them as VAR=VALUE. See below for desc ...

  3. Android检测富文本中的<img标签并实现点击效果

    本文旨在:通过点击一张图片Toast输出位置与url链接. 闲话少说,实现原理大概是酱紫的::通过正则表达式检测富文本内的图片集合并获取url,在src=“xxx” 后面添加 onclick方法,至于 ...

  4. Linux下web服务的搭建

    1.安装Apache Apache的官网地址为:http://httpd.apache.org/,这里以源码的方式进行安装,我们下载的版本是“httpd-2.4.25.tar.gz”,下载后的压缩文件 ...

  5. Sublime Text 2.0.2,Build 2221注册码

    Help ->Enter License,输入如下序列号: ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855 ...

  6. ConcurrentMap

    ConcurrentMap接口下有两个重要的实现: ConcurrentHashMap ConcurrentSkipListMap(支持并发排序功能,弥补ConcurrentHashMap) Conc ...

  7. entityframework删除时提示“DELETE 语句与 REFERENCE 约束”的解决方法。

    1.加入List对象的Include var entity = db.XinDes.Include("Reviews").First(); db.XinDes.Remove(ent ...

  8. python合并图片

    因项目需求需要将图片合并故写了一个python脚本,在这里放个笔记 #!/usr/bin/env python #coding=utf-8 import Image import os import ...

  9. 二安装Python

    因为Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 要开始学习Python编程,首先 ...

  10. XINCLUDE

    前言 导入外部xml文档,类似于php的include,将外部定义的dtd引入当前文件,因为引入外部实体具有局限性,所以使用xinclude来引入 语法 导入外部文档: <xi:include ...