Spring文檔苦讀【7】【Spring Expression Language(SpEL)】

來源:互聯網
上載者:User

標籤:相關   tor   context   運算式   div   argument   result   ssi   birt   

簡介

Spring Expression Language(Spring運算式語言),用來解析java語言中的相關並獲得反饋。

下面就摘錄官方樣本來簡單描述一下。也作一下標記,方便以後查閱。

樣本樣本1
1 ExpressionParser parser = new SpelExpressionParser();2 Expression exp = parser.parseExpression("‘Hello World‘");3 String message = (String) exp.getValue();

變數message的值為:Hello World

樣本2
1 ExpressionParser parser = new SpelExpressionParser();2 Expression exp = parser.parseExpression("‘Hello World‘.concat(‘!‘)");3 String message = (String) exp.getValue();

變數message的值為:Hello World!

樣本3
1 ExpressionParser parser = new SpelExpressionParser();2 3 // invokes ‘getBytes()‘4 Expression exp = parser.parseExpression("‘Hello World‘.bytes");5 byte[] bytes = (byte[]) exp.getValue();
樣本4
1 ExpressionParser parser = new SpelExpressionParser();2 3 // invokes ‘getBytes().length‘4 Expression exp = parser.parseExpression("‘Hello World‘.bytes.length");5 int length = (Integer) exp.getValue();
樣本5
1 ExpressionParser parser = new SpelExpressionParser();2 Expression exp = parser.parseExpression("new String(‘hello world‘).toUpperCase()");3 String message = exp.getValue(String.class);
樣本6
 1 // Create and set a calendar 2 GregorianCalendar c = new GregorianCalendar(); 3 c.set(1856, 7, 9); 4  5 // The constructor arguments are name, birthday, and nationality. 6 Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian"); 7  8 ExpressionParser parser = new SpelExpressionParser(); 9 Expression exp = parser.parseExpression("name");10 11 EvaluationContext context = new StandardEvaluationContext(tesla);12 String name = (String) exp.getValue(context);

擷取對象tesla的屬性name的值。

樣本7
 1 / Create and set a calendar 2 GregorianCalendar c = new GregorianCalendar(); 3 c.set(1856, 7, 9); 4  5 // The constructor arguments are name, birthday, and nationality. 6 Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian"); 7  8 ExpressionParser parser = new SpelExpressionParser(); 9 Expression exp = parser.parseExpression("name == ‘Nikola Tesla‘");10 boolean result = exp.getValue(context, Boolean.class); // evaluates to true

變數result為判斷對象tesla的屬性name是否和字串‘Nikola Tesla‘相同。

 

Spring文檔苦讀【7】【Spring Expression Language(SpEL)】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.