Spring EL hello world实例
1. Spring Beans
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形式
<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>
- #{itemBean} – 注入“itemBean”到“customerBean”Bean 的“item”属性。
- #{itemBean.name} – 注入“itemBean”的“name”属性到 “customerBean" bean的"itemname”属性。
4. Spring EL以注解形式
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>
5. 执行输出
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实例的更多相关文章
- Spring EL方法调用实例
Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用SpEL方法调用”. Spring EL在注解的形式 了解如何实现Spring EL方法 ...
- Spring EL bean引用实例
在Spring EL,可以使用点(.)符号嵌套属性参考一个bean.例如,“bean.property_name”. public class Customer { @Value("#{ad ...
- Spring EL运算符实例
Spring EL支持大多数标准的数学,逻辑和关系运算符. 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , l ...
- Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...
- Spring EL表达式和资源调用
Spring EL表达式 Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于在jsp的EL表达式语言. Spring 开发中经常涉及调用各种资源的情况, ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(九):历史任务查询
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(八):完成个人任务
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
随机推荐
- Eclipse java项目转换为web项目
1.打开.project文件,并修改文件, 修改如下: 找到:<natures> </natures>代码段,在代码段中加入如下内容并保存: <nature>org ...
- php7.33 configure
To assign environment variables (e.g., CC, CFLAGS...), specify them as VAR=VALUE. See below for desc ...
- Android检测富文本中的<img标签并实现点击效果
本文旨在:通过点击一张图片Toast输出位置与url链接. 闲话少说,实现原理大概是酱紫的::通过正则表达式检测富文本内的图片集合并获取url,在src=“xxx” 后面添加 onclick方法,至于 ...
- Linux下web服务的搭建
1.安装Apache Apache的官网地址为:http://httpd.apache.org/,这里以源码的方式进行安装,我们下载的版本是“httpd-2.4.25.tar.gz”,下载后的压缩文件 ...
- Sublime Text 2.0.2,Build 2221注册码
Help ->Enter License,输入如下序列号: ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855 ...
- ConcurrentMap
ConcurrentMap接口下有两个重要的实现: ConcurrentHashMap ConcurrentSkipListMap(支持并发排序功能,弥补ConcurrentHashMap) Conc ...
- entityframework删除时提示“DELETE 语句与 REFERENCE 约束”的解决方法。
1.加入List对象的Include var entity = db.XinDes.Include("Reviews").First(); db.XinDes.Remove(ent ...
- python合并图片
因项目需求需要将图片合并故写了一个python脚本,在这里放个笔记 #!/usr/bin/env python #coding=utf-8 import Image import os import ...
- 二安装Python
因为Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 要开始学习Python编程,首先 ...
- XINCLUDE
前言 导入外部xml文档,类似于php的include,将外部定义的dtd引入当前文件,因为引入外部实体具有局限性,所以使用xinclude来引入 语法 导入外部文档: <xi:include ...