13点睛Spring4.1-Spring EL
13.1 Spring EL
- Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言;
- 本教程关注于在注解中使用Spring EL;
- Spring EL包含很多类型的表达式,本教程关注常用的注入
- 获得系统属性
- 注入表达式
- 注入文件
- 注入其他bean或者其属性
- 注入properties文件属性
- 注入普通字符
13.2 示例
13.2.1 编写Spring EL演示类
@Configuration
@PropertySource("classpath:com/wisely/springel/test.properties")
public class DemoService {
@Value("#{systemProperties}")
private Properties systemProperties; @Value("#{systemProperties['os.name']}")
private String osName; @Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber; @Value("classpath:com/wisely/springel/info.txt")
private Resource info; @Value("#{demoBean.another}")
private String fromAnother;
//注意注入properties使用$而不是#,且需要声明propertyConfigure,在下面的@Bean
@Value("${wisely.name}")
private String myName; @Value("I love iteye and github")
private String normal;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
} //getter }
13.2.2 编写java类测试注入其他bean的属性
package com.wisely.springel; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DemoBean {
@Value("其他类的属性")
private String another; public String getAnother() {
return another;
} public void setAnother(String another) {
this.another = another;
} }
13.2.3 新建测试txt和properties
1234
wisely.name = wyf
13.2.3 测试
package com.wisely.springel;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.springel");
DemoService ds = context.getBean(DemoService.class);
System.out.println("注入操作系统属性:"+ds.getSystemProperties());
System.out.println("注入操作系统名称: "+ds.getOsName());
System.out.println("注入随机数: "+ds.getRandomNumber());
System.out.println("注入文件 "+ds.getInfo().getFilename());
System.out.println("注入其他bean属性: "+ds.getFromAnother());
System.out.println("注入properties文件的属性: "+ds.getMyName());
System.out.println("注入普通字符 "+ds.getNormal());
context.close();
}
}
输出结果:
注入操作系统属性:{java.runtime.name=Java(TM) SE Runtime Environment......}
注入操作系统名称: Windows 8.1
注入随机数: 15.342237263367908
注入文件 info.txt
注入其他bean属性: 其他类的属性
注入properties文件的属性: wyf
注入普通字符 I love iteye and github
13点睛Spring4.1-Spring EL的更多相关文章
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 把功能强大的Spring EL表达式应用在.net平台
Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- Test Spring el with ExpressionParser
Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...
- Spring EL regular expression example
Spring EL supports regular expression using a simple keyword "matches", which is really aw ...
- Spring EL Lists, Maps example
In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...
- Spring EL ternary operator (if-then-else) example
Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...
- Spring EL Operators example
Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...
随机推荐
- AJAX备忘
基础 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJAX ...
- 网络请求之get post
——http get和post的区别? 1.get用于获取数据,post用于提交数据 2.get提交参数追加在url后面,post参数可以通过http body提交 3.get的url会有长度上的限制 ...
- 二扩域(GF(2^m))中的逆矩阵
通常的逆矩阵可以用高斯消去法计算.十分有效.还可以使用LU分解,QR分解等. 二扩域中的逆矩阵则不同.看似简单,其实有别:它的所有元素定义在GF(2^m)中.从理论来看,似乎也可以用高斯消去法,只是计 ...
- vue-d2admin-axios异步请求登录,先对比一下Jquery ajax, Axios, Fetch区别
先说一下对比吧 Jquery ajax, Axios, Fetch区别之我见 引言 前端技术真是一个发展飞快的领域,我三年前入职的时候只有原生XHR和Jquery ajax,我们还曾被JQuery 1 ...
- php unset
说明:unset ( mixed $var [, mixed $... ] ) : void unset() 销毁指定的变量. unset() 在函数中的行为会依赖于想要销毁的变量的类型而有所不同. ...
- Tkinter 之TreeView表格与树状标签
一.TreeView介绍 TreeView组件是一个树状结构和表格的结合体.第一列是树状结构,后几列是列表.每一行表示一个item,树的item可以分级,每个item有子item,名称对应text标签 ...
- Java 面向对象(十五)
Lambda表达式 1. 函数式编程思想概述 在数学中,函数就是有输入量.输出量的一套计算方案,也就是"拿什么东西做什么事情".相对而言,面向对象过分强调"必须通过对象的 ...
- Python ( )、[ ]、{}的区别
python语言最常见的括号有三种,分别是:小括号( ).中括号[ ]和大括号也叫做花括号{ },分别用来代表不同的python基本内置数据类型. python中的小括号( ):代表tuple元组数据 ...
- 全新的Unity跨平台开发 IDE JetBrains Rider 2019.2 x64特别版下载
Rider 基于 JetBrains 的平台,JetBrains 的平台很受那些使用 IntelliJ IDEA 的 Java 开发者和使用 WebStorm 的 JavaScript 开发者的欢迎. ...
- oracle取前10条记录
--oracle取前十条数据 --(1)第一种 ; --(2)第二种 ;