java el表达式实现计算器_Spring EL表示式的运用@Value说明

Spring EL表达式语言,支持在XML和注解中表达式,类是于JSP的EL表达式语言。

在Spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入。

Spring主要在注解@value的参数中使用表达式。

本事咧演示一下情况:

注入普通字符串

注入操作系统属性

注入表达式运算结果

注入其他Bean的属性

注入文件内容

注入网址内容

注入属性文件(注意:用的是$符号)

配置文件test.properties:

book.author=wangyunfei

book.name=spring boot

测试文件test.text:

你好!Spring boot

注入类:

@Configuration // 声明当前类是一个配置类,相当于Spring配置的XML文件

// 包扫描,并排除了对BeanConfig的扫描

@ComponentScan(basePackages={"com.chenfeng.xiaolyuh"}, excludeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value={BeanConfig.class, AopConfig.class})})

@PropertySource("classpath:test.properties")// 指定文件地址

public class ELConfig {

@Value("注入普通字符串")// 注入普通字符串

private String normal;

@Value("#{systemProperties['os.name']}")// 注入操作系统属性

private String osName;

@Value("#{T(java.lang.Math).random() * 100.0 }")// 注入表达式结果

private double randomNumber;

@Value("#{demoELService.another}")// 注入其他Bean属性

private String fromAnother;

@Value("classpath:test.txt")// 注入文件资源

private Resource testFile;

@Value("https://www.baidu.com")// 注入网址资源

private Resource testUrl;

@Value("${book.name}")// 注入配置文件【注意是$符号】

private String bookName;

@Autowired// Properties可以从Environment获得

private Environment environment;

// @Bean

// public static PropertySourcesPlaceholderConfigurer propertyConfigure() {

// return new PropertySourcesPlaceholderConfigurer();

// }

@Override

public String toString() {

try {

return "ELConfig [normal=" + normal

+ ", osName=" + osName

+ ", randomNumber=" + randomNumber

+ ", fromAnother=" + fromAnother

+ ", testFile=" + IOUtils.toString(testFile.getInputStream())

+ ", testUrl=" + IOUtils.toString(testUrl.getInputStream())

+ ", bookName=" + bookName

+ ", environment=" + environment.getProperty("book.name") + "]";

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

}

测试类:

public class SpringELTest {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class);

@Test

public void contextTest() {

ELConfig elConfig = context.getBean(ELConfig.class);

System.out.println(elConfig.toString());

}

@After

public void closeContext() {

context.close();

}

}

补充知识:yml、properties获取pom自定义变量

pom变量:

dev

dev

jdbc:mysql://127.0.0.1:3306/melab?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai

jdbc:mysql://127.0.0.1:3306/tx-manager?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai

root

123456

yml获取pom变量:

添加依赖:

org.yaml

snakeyaml

1.25

获取变量:

url: @jdbc-url@

lcn-log-url: @jdbc-url@

username: @jdbc-user@

password: @jdbc-password@

properties获取pom变量:

build设置:

maven-resources-plugin

utf-8

true

获取变量:

spring.datasource.url=${jdbc-url}

spring.datasource.username=${jdbc-user}

spring.datasource.password=${jdbc-password}

以上这篇Spring EL表示式的运用@Value说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

时间: 2020-04-25

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值