Spring EL regular expression example

Spring EL supports regular expression using a simple keyword “matches“, which is really awesome! For examples,

@Value("#{'100' matches '\\d+' }")  
private boolean isDigit;

It test whether ‘100‘ is a valid digit via regular expression ‘\\d+‘.

Spring EL in Annotation

See following Spring EL regular expression examples, some mixed with ternary operator, which makes Spring EL pretty flexible and powerful.
Below example should be self-explanatory.

package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

    // email regular expression
    String emailRegEx = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)" +
                                       "*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    // if this is a digit?
   @Value("#{'100' matches '\\d+' }")
    private boolean validDigit;

    // if this is a digit + ternary operator
    @Value("#{ ('100' matches '\\d+') == true ? " +
                   "'yes this is digit' : 'No this is not a digit'  }")
    private String msg;

    // if this emailBean.emailAddress contains a valid email address?
    @Value("#{emailBean.emailAddress matches customerBean.emailRegEx}")
     private boolean validEmail;

     //getter and setter methods, and constructor
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("emailBean")
public class Email {    
    @Value("nospam@abc.com")    
    String emailAddress;    
    //...
}

Output

Customer [isDigit=true, msg=yes this is digit, isValidEmail=true]

Spring EL in XML

See equivalent version in bean definition XML file.

<beans xmlns="http://www.springframework.org/schema/beans"  
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
             xsi:schemaLocation="http://www.springframework.org/schema/beans    
                                                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
     <bean id="customerBean" class="com.mkyong.core.Customer">   
         <property name="validDigit" value="#{'100' matches '\d+' }" />  
        <property name="msg" value="#{ ('100' matches '\d+') == true ? 'yes this is digit' : 'No this is not a digit' }" />  
        <property name="validEmail" value="#{emailBean.emailAddress matches '^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$' }" />   
    </bean> 
    <bean id="emailBean" class="com.mkyong.core.Email">  
        <property name="emailAddress" value="nospam@abc.com" /> 
    </bean>
</beans>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,500評論 19 139
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan閱讀 5,531評論 7 56
  • 我很久以前就聽說寫作的好處。而且現(xiàn)在自媒體創(chuàng)業(yè)很火,知識變現(xiàn),內容創(chuàng)業(yè)看看微信上好像滿天飛的知識變現(xiàn)百萬富翁。...
    諸葛妙計閱讀 317評論 0 2
  • 她坐在彩虹下 傷懷于烤干的土痂 撲在他懷里哭訴 “不見了,嗚~專一的雨洼?!?他輕輕地環(huán)抱她 撫弄著溫柔的頭發(fā) 看...
    露楓閱讀 288評論 0 0
  • 老船木越來越被人熟知,但是真正了解的人不多,到底要不要選擇老船木呢?為什么會有越來越多的人選擇它?我們的家合適用老...
    ff695ff9698f閱讀 547評論 0 0

友情鏈接更多精彩內容