资源调用java
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.Resource; @PropertySource("classpath:xxx.properties") public class Test { @Value("aaa") private String a; @Value("#{systemProperties['os.name']}") //系统属性 private String b; @Value("#{T(java.lang.Math).random()*110.0}") //表达式 private double c; @Value("#{bean.aa}") //bean属性 private String d; @Value("classpath:xxx") private Resource e; @Value("http://xxx") private Resource f; @Value("${xx.xx}") //配置文件 private String g; }
spring ELspring
public static void main(String[] args) { //ExpressionParser :该接口的实例负责解析一个SpEL表达式,返回一个Expression对象。 //Expression :该接口的实例表明一个表达式 //EvaluationContext :表明计算表达式值的上下。当SpEL表达式中有变量时,程序须要使用该API来计算表达式值。 //Expression#getValue() Object //Expression#getValue(T) T //Expression#getValue(EvaluationContext) Object //Expression#getValue(EvaluationContext,T) T //Expression#getValue(Object rootObject) Object //Expression#getValue(Object rootObject,T) T ExpressionParser parser = new SpelExpressionParser(); //例1: 字符串表达式 <-> "hello" Expression expression = parser.parseExpression("'hello'"); System.out.println(expression.getValue()); //hello //例2: 方法表达式 <-> "hello".concat("!") expression = parser.parseExpression("'hello'.concat('!')"); System.out.println(expression.getValue()); //hello! //例3: getter表示式 <-> "hello".getBytes() expression = parser.parseExpression("'hello'.bytes"); System.out.println(expression.getValue()); //[B@3327bd23 //例4:对象属性 <-> "hello".getBytes().length expression = parser.parseExpression("'hello'.bytes.length"); System.out.println(expression.getValue()); //5 //例5:建立对象 <-> new String("sdf"); expression = parser.parseExpression("new String('hello')"); System.out.println(expression.getValue()); //hello User user = new User("张三"); //例6:以指定对象做为root来计算值 <-> user.getName() expression = parser.parseExpression("name"); System.out.println(expression.getValue(user)); //张三 StandardEvaluationContext context = new StandardEvaluationContext(); //例7:以指定context来计算值 <-> user.getName().equals("张三") context.setRootObject(user); expression = parser.parseExpression("name=='张三'"); System.out.println(expression.getValue(context)); //true //例8:context 其余参数 <-> list.get(0) List<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); context.setVariable("list", list); expression = parser.parseExpression("#list[0]"); //a System.out.println(expression.getValue(context)); //例9:修改变量值 parser.parseExpression("#list[0]").setValue(context, "ee"); System.out.println(list); // [ee, b, c] //---------------语法---------------- parser.parseExpression("'hello'"); //字符串 parser.parseExpression("0.23"); //浮点数 parser.parseExpression("new String[]{'a','b','c'}"); //数组 parser.parseExpression("{'a','b','c'}"); //list parser.parseExpression("{{'a','b','c'},{'d','e'}}"); //二维集合 //list[index] 集合访问 //map[key] map访问 //list[0]='a' 对集合的第一个元素进行赋值 //类运算符 T parser.parseExpression("T(java.lang.Math).random()"); //T()运算符使用java.lang包下的类时能够省略包名 //调用构造器 parser.parseExpression("new String('hello')"); //变量,SpEL使用 EvaluationContext 来使用变量,能够使用 #name来访问变量名 parser.parseExpression("#user.name"); parser.parseExpression("#this"); //引用SpEL当前正在计算的对象 parser.parseExpression("#root"); //引用SpEL的EvaluationContext root对象 //自定义函数 //经过 StandardEvaluationContext 的以下方法便可在 SpEL 中注册自定义的函数 //context.registerFunction(name,method); //Elvis 运算符 // name!=null ? name: "newVal" // name?:"newVal" //安全导航操做 //user?.name 若是user 为 null,不会报错 parser.parseExpression("#user?.name"); //集合选择 collections.?[条件] list = new ArrayList<>(); list.add("aa"); list.add("bbb"); list.add("cccc"); Map<String, Integer> map = new HashMap<>(); map.put("aa", 1); map.put("bbb", 2); map.put("cccc", 3); context.setVariable("list", list); context.setVariable("map", map); //判断集合元素 length() 方法长度在于 7 :cccc 将被移除 parser.parseExpression("#list.?[length()>3]"); //判断map 的 value 值大于 2 : 只剩 ccc parser.parseExpression("#map.?[value>2]"); //也能够调用 key //集合投影 collections.![条件] //获得的新集合的元素是原集合的每一个元素length()方法返回值 parser.parseExpression("list.![length()]"); List<User> users = new ArrayList<>(); users.add(new User("张三")); users.add(new User("李四")); context.setVariable("users", users); parser.parseExpression("users.![name]"); //表达式模板 ,须要使用 TemplateParserContext 对象 expression = parser.parseExpression("用户名是#{name}", new TemplateParserContext()); System.out.println(expression.getValue(new User("张三"))); //用户名是张三 } static class User { private String name; public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }