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. learning java Cloneable

    class Address{ String Detail; public Address(String detail){ this.Detail = detail; } } class User im ...

  2. c函数指针和指针函数如何使用何定义;如何调用使用

    #include <stdio.h> int * sum(int x); //声明一个 指针函数 返回类型位一个指针变量 可以通过*p来获取值 int (*pfun)(int,int);/ ...

  3. Windows 10操作系统中的邮件应用

    早期的邮件收发软件Outlook express曾被广泛使用,然而现在却很难下载到,在Windows 10操作系统中也无法使用. 实际上,Windows 10中存在“邮件”应用,可以直接作为邮件的收发 ...

  4. 一次docker镜像的迁移

    docker 镜像迁移 背景,本地测试环境要切到线上测试,镜像下载或编译都需要时间. 所以直接scp镜像过去来节省时间. save 相对于export会占用更多存储空间 被迁移服务器导出所有镜像 do ...

  5. 本地spark报:java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.createFileWithMode0(Ljava/lang/String;JJJI)Ljava/io/FileDescriptor;

    我是在运行rdd.saveAsTextFile(fileName)的时候报的错,找了很多说法……最终是跑到hadoop/bin文件夹下删除了hadoop.dll后成功.之前某些说法甚至和这个解决方法自 ...

  6. Docker配置文件详解

    先来看一份 docker-compose.yml 文件,不用管这是干嘛的,只是有个格式方便后文解说: version: '2' services: web: image: dockercloud/he ...

  7. LeetCode 第 154 场周赛

    一."气球" 的最大数量(LeetCode-5189) 1.1 题目描述 1.2 解题思路 统计各个字母的出现的次数,然后根据"木桶最短板"返回就好. 1.3 ...

  8. kafka集群搭建(图文并用)

    将安装包上传服务器并解压 scp kafka_2.11-1.0.0.tgz username@{ip}:~/. mkdir /usr/local/kafka mv kafka_2.11-1.0.0.t ...

  9. html5表单重写

    html5表单重写 一.总结 一句话总结: 表单重写用于在提交按钮上指定表单提交的各种信息,比如action <input type="submit" value=" ...

  10. 范仁义web前端介绍课程---5、webstorm的下载安装

    范仁义web前端介绍课程---5.webstorm的下载安装 一.总结 一句话总结: webstorm破解版搜索:webstorm破解 site:52pojie.cn 编辑器随便选用,功能都差不多,哪 ...