spring EL表达式

spring EL表达式需要放入:#{…}
属性占位符需要放入:#{…}

package springBase.springEL;

import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

@Configuration
@ComponentScan("springBase.springEL")
@PropertySource("classpath:springBase/springEL/test.properties")
public class JavaConfig {
	@Value("I Love You") // 注入普通字符串
	private String normal;

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

	@Value("#{100 * 100.0}") // 注入表达式结果
	private String num;

	@Value("#{demoService.demoValue>101?'win':'lose'}") // 三目运算符
	private String ternaryOperator;

	@Value("#{demoService.nullvalue?:'default'}") // 三目运算符的null字符检测demoService.nullvalue为null则取default
	private String ternaryOperatorNull;

	@Value("#{T(java.lang.Math).random()}") // 注入java类方法
	private String random;
	
	@Value("#{demoService.demoValue matches '\\d*'}") // 正则表达式
	private boolean regular;

	@Value("#{demoService.nullvalue?.toUpperCase()}") // ?.表示如果demoService.nullvalue为null则不调用toUpperCase,直接返回null
	private String ifnull;

	@Value("#{demoService.another}") // 注入表达式结果
	private String fromAnother;
	
	@Value("#{demoService.list[0]}") // 注入集合
	private String list0;
	
	@Value("1")
	private int index;
	
	@Value("#{demoService.list[javaConfig.index]}") // 注入集合,使用其他已经注入的值作为index
	private String list1;
	
	@Value("#{'hello'[3]}") // 从字符串中获取指定字符,返回'l'字符
	private String hello3;
	
	@Value("#{demoService.students.?[name eq 's1']}") // 获取集合中对象的name属性和's1'进行比较,所有符合条件的放入students集合中
	private List<Student> students;
	
	@Value("#{demoService.students.^[name eq 's1']}") // 获取集合中对象的name属性和's1'进行比较,第一个符合条件的放入student中
	private Student firstStudent;
	
	@Value("#{demoService.students.$[name eq 's1']}") // 获取集合中对象的name属性和's1'进行比较,最后一个符合条件的放入student中
	private Student lastStudent;
	
	@Value("#{demoService.students.![name]}") // 投影运算符,将Student中的name属性放入集合中
	private List<String> names;
	
	@Value("#{demoService.students.?[name eq 's1'].![name]}") // 混合使用投影运算符和其他运算符,必须保证使用投影运算符前的结果是一个集合
	private List<String> filterNames;

	@Value("classpath:springBase/springEL/test.txt") // 注入文件资源
	private Resource testFile;

	@Value("http://www.baidu.com") // 注入网址资源
	private Resource testUrl;

	@Value("${book.name}") // 注入配置文件
	private String bookName;

	@Autowired
	private Environment environment;// 注入配置文件,使用占位符方式

	public void ouputResource() {
		System.out.println(normal);
		System.out.println(osName);
		System.out.println(num);
		System.out.println(fromAnother);
		System.out.println(random);
		System.out.println(ifnull);
		System.out.println(ternaryOperator);
		System.out.println(ternaryOperatorNull);
		System.out.println(regular);
		System.out.println(list0);
		System.out.println(list1);
		try {
			System.out.println(IOUtils.toString(testFile.getInputStream(), "UTF-8"));
			System.out.println(IOUtils.toString(testUrl.getInputStream(), "UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(bookName);
		System.out.println("使用占位符方式:" + environment.getProperty("book.name"));
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值