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, ...
随机推荐
- PostgreSQL 索引坏块处理
今天应用反应有张表查询报错,报错信息如下 back=# select max(create_time) from public.tbl_index_table where create_time> ...
- 06.volatile关键字
volatile volatile关键字的主要作用是使变量在多个线程间可见 使用方法: private volatile int number=0; 图示: 两个线程t1和t2共享一份数据,int a ...
- DVWA-弱会话ID
本周学习内容: 1.学习web安全深度剖析: 2.学习安全视频: 3.学习乌云漏洞: 4.总结Web应用安全权威指南: 实验内容: 进行DVWA弱会话ID实验 实验步骤: Low 1.打开DVWA,进 ...
- Springboot如何优雅的解决ajax+自定义headers的跨域请求[转]
1.什么是跨域 由于浏览器同源策略(同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略.所谓同源是指,域名,协议,端口相同.),凡是发 ...
- 洛谷 P1613 跑路 题解
P1613 跑路 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的 ...
- LOJ575. 「LibreOJ NOI Round #2」不等关系 [容斥,分治FFT]
LOJ 思路 发现既有大于又有小于比较难办,使用容斥,把大于改成任意减去小于的. 于是最后的串就长成这样:<<?<?<??<<<?<.我们把一段连续的& ...
- Pytest权威教程08-使用tmp目录和文件
目录 使用tmp目录和文件 tmp_path Fixture方法 tmp_path_factory Fixture方法 tmpdir Fixture方法 tmpdir_factory Fixture方 ...
- 6、httpd2.4 编译安装LAMP
www.itjc8.com 新特性: MPM支持运营DSO机制(动态共享对象),以模块形式按需加载 支持event MPM 支持异步读写 支持每模块及每个目录分别使用各自的日志级别 每请求配置 增强版 ...
- 一段js MD5。加密 转换C#语法过程
A 帮忙把这段js脚本转换 c#语言. JS: function md5 (bit,sMessage) {debugger //var sMessage = this; function Rotate ...
- Mac和window生成ssh和查看ssh key
一.MAC系统 mac 系统开始就已经为我们安装了ssh 如果没有安装,首先安装 打开终端:$ ssh -v 查看ssh版本 OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec ...