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, ...
随机推荐
- RestTemplate 使用中的几个问题
Spring Boot使用RestTemplate消费REST服务的几个问题记录 我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调 ...
- Linux https认证原理
HTTPS在传输的过程中会涉及到三个密钥:服务器端的公钥和私钥,用来进行非对称加密客户端生成的随机密钥,用来进行对称加密一个HTTPS请求实际上包含了两次HTTP传输,可以细分为8步.1.客户端向服务 ...
- VS - ActionFilterAttribute
Global.asax.cs public class MvcApplication : System.Web.HttpApplication { public static void Registe ...
- (尚009)Vue列表渲染
变异方法:说白了就是对原方法进行了包装,包装后实现了2个功能1:实现原方法的功能;2.更新界面. 1.test009.html <!DOCTYPE html><html lang=& ...
- learning java AWT BoxLayout布局管理器
import javax.swing.*; import java.awt.*; public class BoxSpaceTest { private Frame f = new Frame(&qu ...
- loj #137 and #6021
最小瓶颈路 加强版 重构树 最小生成树在合并 (x, y) 时,新建节点 z,link(x, z), link(y, z), 新建节点的权值为 w_{x,y}, 这样的 话任意两点的 answer 为 ...
- 机器学习---用python实现最小二乘线性回归算法并用随机梯度下降法求解 (Machine Learning Least Squares Linear Regression Application SGD)
在<机器学习---线性回归(Machine Learning Linear Regression)>一文中,我们主要介绍了最小二乘线性回归算法以及简单地介绍了梯度下降法.现在,让我们来实践 ...
- Pytest权威教程01-安装及入门
目录 安装及入门 安装 Pytest 创建你的第一个测试用例 执行多条测试用例 断言抛出了指定异常 使用类组织多条测试用例 函数测试中请求使用独立的临时目录 进一步阅读 返回: Pytest权威教程 ...
- mac 安装cmake
下载:https://cmake.org/download/ 下载完成后,双击安装 安装完成后,打开命令行,运行 bogon:~ macname$ sudo "/Applications/C ...
- 怎么用switchhost
第一步:打开exe, 第二部:在 My hosts 里面直接添加路径 106.75.131.183 npm.kuaizitech.cn 第三部 :打开my hosts 保护好眼睛,早睡早起,多运动,k ...