1. 程式人生 > >Spring的斷言工具類Assert的基本使用

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

Assert.notNull(Object object, "object is required")    -    物件非空 
Assert.isTrue(Object object, "object must be true")   -    物件必須為true   
Assert.notEmpty(Collection collection, "collection must not be empty")    -    集合非空  
Assert.hasLength(String text, "text must be specified")   -    字元不為null且字元長度不為0   

Assert.hasText(String text, "text must not be empty")    -     text 不為null且必須至少包含一個非空格的字元  

Assert.isInstanceOf(Class clazz, Object obj, "clazz must be of type [clazz]")    -    obj必須能被正確造型成為clazz 指定的類

可以把if(判斷){}改為斷言