spring的斷言工具類Assert的基本使用

<div class="htmledit_views">html

<p>這幾天比較閒看了下之前的項目,發現了這個spring下的Assert方法,(之前用過,不過好像忘的差很少了*.*)</p> <p>org.springframework.util.Assert;</p> <p>Assert斷言工具類,一般用於數據合法性檢查.<br></p> <p>平時作判斷一般都是這樣寫</p> <p>if (message== null || message.equls("")) { &nbsp;<br> &nbsp; &nbsp; throw new IllegalArgumentException("輸入信息錯誤!"); &nbsp;<br> }&nbsp;<br></p> <p>用Assert工具類上面的代碼能夠簡化爲: &nbsp;<br> &nbsp;&nbsp;<br> Assert.hasText((message, "輸入信息錯誤!");<br></p> <p><br></p> <p>下面來介紹我收集的一下Assert 類中的經常使用斷言方法: &nbsp;<br> &nbsp;<br> Assert.notNull(Object object, "object is required") &nbsp; &nbsp;- &nbsp; &nbsp;對象非空&nbsp;<br> Assert.isTrue(Object object, "object must be true") &nbsp; - &nbsp; &nbsp;對象必須爲true &nbsp;&nbsp;<br> Assert.notEmpty(Collection collection, "collection must not be empty") &nbsp; &nbsp;- &nbsp; &nbsp;集合非空 &nbsp;<br> Assert.hasLength(String text, "text must be specified") &nbsp; - &nbsp; &nbsp;字符不爲null且字符長度不爲0 &nbsp;&nbsp;<br> Assert.hasText(String text, "text must not be empty") &nbsp; &nbsp;- &nbsp; &nbsp; text 不爲null且必須至少包含一個非空格的字符 &nbsp;<br> Assert.isInstanceOf(Class clazz, Object obj, "clazz must be of type [clazz]") &nbsp; &nbsp;- &nbsp; &nbsp;obj必須能被正確造型成爲clazz 指定的類<br></p> </div>spring

相關文章
相關標籤/搜索