Spring 断言Assert的基本使用和方法

前言

用assert 关键字来限制参数传递(检验方法参数的有效性)

使用

Assert.notNull(list,"集合不存在");

在这里插入图片描述
格式参数:(assert booleanExpression,assert booleanExpression:messageExpression)
1.booleanExpression当值为true时,程序从断言语句处继续执行;
2.booleanExpression当值为false时,程序从断言语句处停止执行,并输出messageException表达式的值;

日常方法

  1. Assert.notNull(Object object, “object is required”) - 对象非空
  2. Assert.isTrue(Object object, “object must be true”) - 对象必须为true
  3. Assert.notEmpty(Collection collection, “collection must not be empty”) - 集合非空
  4. Assert.hasLength(String text, “text must be specified”) - 字符不为null且字符长度不为0
  5. Assert.hasText(String text, “text must not be empty”) - text 不为null且必须至少包含一个非空格的字符
  6. Assert.isInstanceOf(Class clazz, Object obj, “clazz must be of type [clazz]”) - obj必须能被正确造型成为clazz 指定的类

猜你喜欢

转载自blog.csdn.net/qq_28545605/article/details/125762389
今日推荐