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

相关文章
相关标签/搜索