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的更多相关文章

  1. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  2. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  3. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

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

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

  5. Test Spring el with ExpressionParser

    Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...

  6. Spring EL regular expression example

    Spring EL supports regular expression using a simple keyword "matches", which is really aw ...

  7. 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 ...

  8. Spring EL ternary operator (if-then-else) example

    Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...

  9. Spring EL Operators example

    Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...

随机推荐

  1. 012——软件安装之_matlab2019安装

    (一)参考文献:https://www.isharepc.com/14196.html (二)下载地址: 链接:https://pan.baidu.com/s/1gq06TuBWGr1Qc4owpRX ...

  2. Go Node.js 生成的exe公布成windows服务

    环境变量 GOBIN E:\01_SOFT\go1.9.2\bin GOROOT E:\01_SOFT\go1.9.2 GOPATH(下载包的存放位置:go get github.com/gin-go ...

  3. 给各阶段java学习者的建议[转]

    第一部分:零基础或基础薄弱的同学这部分主要适用于尚未做过Java工作的同学,包括一些在校生以及刚准备转行Java的同学.一.Java基础首先去找一个Java的基础教程学一下,这里可以推荐达内java课 ...

  4. 2017.10.3 国庆清北 D3T3 解迷游戏

    题目描述 LYK进了一家古董店,它很想买其中的一幅画.但它带的钱不够买这幅画. 幸运的是,老板正在研究一个问题,他表示如果LYK能帮他解出这个问题的话,就把这幅画送给它. 老板有一个n*m的矩阵,他想 ...

  5. redis系列(五):搭建redis-cluster集群

    1.为什么要用redis-cluster a.并发要求 redis官方声称可以达到10万每秒,但是如果业务需要每秒100万条呢?b.数据量太大 一台服务器的内存正常是16-256G,如果业务需要500 ...

  6. 《挑战30天C++入门极限》新手入门:C/C++中数组和指针类型的关系

        新手入门:C/C++中数组和指针类型的关系 对于数组和多维数组的内容这里就不再讨论了,前面的教程有过说明,这里主要讲述的数组和指针类型的关系,通过对他们之间关系的了解可以更加深入的掌握数组和指 ...

  7. 9.本地线程(ThreadLoca)

    ThreadLoca 提高一个线程的局部变量,访问某个线程都有自己的局部变量,当使用ThreadLoca为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立的改变自己的副本,二不会影响到 ...

  8. python3+requests get/post 请求

    1.get请求 (1)没有请求参数类型 1 response = requests.get(url='') 2 print(response.text) (2)有请求参数的类型(键值对形式表示参数) ...

  9. ubuntu16.04安装python3.7

    1.安装依赖包 sudo apt-get update sudo apt-get install build-essential python-dev python-setuptools python ...

  10. VS2008下LibTomCrypt 1.17的编译和使用《转》

    文章出处:http://blog.csdn.net/delphiwcdj/article/details/6298820 具体步骤如下: (1) 下载tomcrypt tomcrypt 1.17 VS ...