spring EL 和資源調用

資源調用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;
    }
}
相關文章
相關標籤/搜索