spring-expression Spel表達式 java語言解析

對表達式進行解析,對於一開始不知道在哪用的方法,通過字符串傳入解析器,進行解析。

demo 1

解析"1+2"

public static void main(String[] args) {
        //將一整個語句直接定義了字符串 ,其中對字符串的開始索引與結束索引使用替代變量
        String str = "1 + 2";
        //1定義一個專屬的表達式解析工具
        ExpressionParser parser = new SpelExpressionParser() ;
        //2定義一個表達式處理類
        Expression exp = parser.parseExpression(str);
        //3進行最終的表達式計算
        EvaluationContext context = new StandardEvaluationContext() ;
        //4通過表達式進行結果計算
        System.out.println(exp.getValue(context));
    }

輸出如下:

3

 

demo2

對方法進行解析

public class C {
	public static void main(String[] args) {
        //new D()爲root根對象,context 爲上下文
		StandardEvaluationContext context = new StandardEvaluationContext(new D());
        #設置解析器
		ExpressionParser parser=new SpelExpressionParser();
        #上下文設置變量
		context.setVariable("name", "Hello");
        //#name用於接收context中設置的變量,a()爲context中設置的對象的方法。
		Object o=parser.parseExpression("a(#name)").getValue(context);
		System.out.println(o);
	}
}

class D{
		public String a(String name){
			System.out.println("@@@@@@@@@@"+name);
			return "#######"+name;
		}
	}

打印結果如下:

@@@@@@@@@@Hello
#######Hello

 

用於用戶接口權限驗證的例子

 

(1)定義一個註解

PreAuth

(2)然後在需要的controller上添加註解,hasPermission中的內容爲接口對應的編碼

(3)定義一個hasPermissio方法。

boolean hasPermissio(resource_code){裏面爲判斷當前用戶有沒有resource_code,返回boolean}

(4)然後試用aop攔截註解,對包含PreAuth註解的方法執行之前進行調用hasPermission驗證有沒有權限,若無拋出異常

 

網上的例子

任何語言都需要有自己的語法,SpEL當然也不例外。所以我們應該能夠想到,給一個字符串最終解析成一個值,這中間至少得經歷:
字符串 -> 語法分析 -> 生成表達式對象 -> (添加執行上下文) -> 執行此表達式對象 -> 返回結果

關於SpEL的幾個概念:

表達式(“幹什麼”):SpEL的核心,所以表達式語言都是圍繞表達式進行的
解析器(“誰來幹”):用於將字符串表達式解析爲表達式對象
上下文(“在哪幹”):表達式對象執行的環境,該環境可能定義變量、定義自定義函數、提供類型轉換等等
root根對象及活動上下文對象(“對誰幹”):root根對象是默認的活動上下文對象,活動上下文對象表示了當前表達式操作的對象
 

 

 

表達式步驟

 

    public static void main(String[] args) {
        //將一整個語句直接定義了字符串
        String str = "(\"Hello \" + \"World!!!\").substring(6, 9)";
        //1定義一個專屬的表達式解析工具
        ExpressionParser parser = new SpelExpressionParser() ;
        //2定義一個表達式處理類
        Expression exp = parser.parseExpression(str);
        //3進行最終的表達式計算
        EvaluationContext context = new StandardEvaluationContext() ;
        //4通過表達式進行結果計算
        System.out.println(exp.getValue());
        
        System.out.println(str);
    }
    

 

使用變量

 

    public static void main(String[] args) {
        //將一整個語句直接定義了字符串 ,其中對字符串的開始索引與結束索引使用替代變量
        String str = "(\"Hello \" + \"World!!!\").substring(#start, #end)";
        //1定義一個專屬的表達式解析工具
        ExpressionParser parser = new SpelExpressionParser() ;
        //2定義一個表達式處理類
        Expression exp = parser.parseExpression(str);
        //3進行最終的表達式計算中進行替代棉量內容的設置
        EvaluationContext context = new StandardEvaluationContext() ;
        context.setVariable("start", 6);
        context.setVariable("end", 9);
        //4通過表達式進行結果計算
        System.out.println(exp.getValue(context));
    
    }

 

簡單表達式

 

    public static void main(String[] args) {
        //將一整個語句直接定義了字符串 ,其中對字符串的開始索引與結束索引使用替代變量
        String str = "1 + 2";
        //1定義一個專屬的表達式解析工具
        ExpressionParser parser = new SpelExpressionParser() ;
        //2定義一個表達式處理類
        Expression exp = parser.parseExpression(str);
        //3進行最終的表達式計算中進行替代棉量內容的設置
        EvaluationContext context = new StandardEvaluationContext() ;
        //4通過表達式進行結果計算
        System.out.println(exp.getValue(context));
    }

 

自定義分割符

 

    public static void main(String[] args) {
        //將一整個語句直接定義了字符串 ,其中對字符串的開始索引與結束索引使用替代變量
        String str = "#[1 + 2]";
        //1定義一個專屬的表達式解析工具
        ExpressionParser parser = new SpelExpressionParser() ;
        //2定義一個表達式處理類                         邊界匹配
        Expression exp = parser.parseExpression(str,new ParserContext() {
            
            @Override
            public boolean isTemplate() {
                return true;
            }
            
            @Override
            public String getExpressionSuffix() {
                return "]";
            }
            
            @Override
            public String getExpressionPrefix() {
                return "#[";
            }            
        });
        //3進行最終的表達式計算中進行替代棉量內容的設置
        EvaluationContext context = new StandardEvaluationContext() ;
        context.setVariable("start", 6);
        context.setVariable("end", 9);
        //4通過表達式進行結果計算
        System.out.println(exp.getValue(context));
    }

 

基本表達式

 

    public static void main(String[] args) {
        //將一整個語句直接定義了字符串 ,其中對字符串的開始索引與結束索引使用替代變量
        String str = "null";
        //1定義一個專屬的表達式解析工具
        ExpressionParser parser = new SpelExpressionParser() ;
        //2定義一個表達式處理類              
        Expression exp = parser.parseExpression(str);
        //3進行最終的表達式計算中進行替代棉量內容的設置
        EvaluationContext context = new StandardEvaluationContext() ;
        //4通過表達式進行結果計算
        Object num = exp.getValue(context ,Object.class); 
        System.out.println(num);
    }

 

+

四則運算

^  冪運算 2^3

% MOD

/  DIV

> GT

>= GE

== EQ

!=   NE

<  LT

<= LE

!   NOT

AND

OR

正則  如  "'123' matches '\\d{3}'"

三木運算   null==null ?'Hello' : 'world'

簡單三目運算         true ? : false     

          null ? : false

 

class表達式

//        String str = "T(String)"; //Class
//        String str = "T(Integer).MAX_VALUE"; //靜態屬性
//        String str = "T(Integer).parseInt('123')"; //靜態方法
//        String str = "new java.util.Date()";     //實例化對象
//        String str = "'hello' instanceof T(String)"; //instanceof

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章