org.springframework.util.Assert

本文介绍如何利用Spring框架中的Assert类简化方法入参检测逻辑,避免重复编写检测代码,提升代码可读性和维护性。通过使用Assert类提供的断言方法,如notNull、hasText等,开发者可以方便地验证参数的有效性,防止因非法参数导致的程序异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法入参监测工具类

Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回。类似的,当我们在编写类的方法时,也常常需要对方法入参进行合法性检查,如果入参不符合要求,方法将通过抛出异常的方式拒绝后续处理。举一个例子:有一个根据文件名获取输入流的方法:InputStream getData(String file),为了使方法能够成功执行,必须保证 file 入参不能为 null 或空白字符,否则根本无须进行后继的处理。这时方法的编写者通常会在方法体的最前面编写一段对入参进行检测的代码,如下所示:

public InputStream getData(String file) {
    if (file == null || file.length() == 0|| file.replaceAll("\s", "").length() == 0) {
        throw new IllegalArgumentException("file入参不是有效的文件地址");
    }

}

类似以上检测方法入参的代码是非常常见,但是在每个方法中都使用手工编写检测逻辑的方式并不是一个好主意。阅读 Spring 源码,您会发现 Spring 采用一个 org.springframework.util.Assert 通用类完成这一任务。

Assert 翻译为中文为“断言”,使用过 JUnit 的读者都熟知这个概念,它断定某一个实际的运行值和预期想一样,否则就抛出异常。Spring 对方法入参的检测借用了这个概念,其提供的 Assert 类拥有众多按规则对方法入参进行断言的方法,可以满足大部分方法入参检测的要求。这些断言方法在入参不满足要求时就会抛出 IllegalArgumentException。下面,我们来认识一下 Assert 类中的常用断言方法:

断言方法说明
notNull(Object object) 当 object 不为 null 时抛出异常,notNull(Object object, String message) 方法允许您通过 message 定制异常信息。和 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null;
isTrue(boolean expression) / isTrue(boolean expression, String message) 当 expression 不为 true 抛出异常;
notEmpty(Collection collection) / notEmpty(Collection collection, String message) 当集合未包含元素时抛出异常。notEmpty(Map map) / notEmpty(Map map, String message) 和 notEmpty(Object[] array, String message) / notEmpty(Object[] array, String message) 分别对 Map 和 Object[] 类型的入参进行判断;
hasLength(String text) / hasLength(String text, String message) 当 text 为 null 或长度为 0 时抛出异常;
hasText(String text) / hasText(String text, String message) text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;
isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message) 如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;
isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message) subType 必须可以按类型匹配于 superType,否则将抛出异常;

使用 Assert 断言类可以简化方法入参检测的代码,如 InputStream getData(String file) 在应用 Assert 断言类后,其代码可以简化为以下的形式:

public InputStream getData(String file){ Assert.hasText(file,"file入参不是有效的文件地址"); 使用 Spring 断言类进行方法入参检测 … }

可见使用 Spring 的 Assert 替代自编码实现的入参检测逻辑后,方法的简洁性得到了不少的提高。Assert 不依赖于 Spring 容器,您可以大胆地在自己的应用中使用这个工具类。

 

转自:http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils2/

2025-07-08T18:21:59.036+08:00 ERROR 18376 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.plugin.core.support.AbstractTypeAwareSupport$BeansOfTypeTargetSource.<init>(AbstractTypeAwareSupport.java:135) The following method did not exist: 'void org.springframework.util.Assert.notNull(java.lang.Object)' The calling method's class, org.springframework.plugin.core.support.AbstractTypeAwareSupport$BeansOfTypeTargetSource, was loaded from the following location: jar:file:/D:/maven3/maven-repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar!/org/springframework/plugin/core/support/AbstractTypeAwareSupport$BeansOfTypeTargetSource.class The called method's class, org.springframework.util.Assert, is available from the following locations: jar:file:/D:/maven3/maven-repository/org/springframework/spring-core/6.1.10/spring-core-6.1.10.jar!/org/springframework/util/Assert.class The called method's class hierarchy was loaded from the following locations: org.springframework.util.Assert: file:/D:/maven3/maven-repository/org/springframework/spring-core/6.1.10/spring-core-6.1.10.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.plugin.core.support.AbstractTypeAwareSupport$BeansOfTypeTargetSource and org.springframework.util.Assert 进程已结束,退出代码为 0
最新发布
07-10
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-06-19T02:37:54.443+08:00 ERROR 15392 --- [demo] [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.plugin.core.support.AbstractTypeAwareSupport$BeansOfTypeTargetSource.<init>(AbstractTypeAwareSupport.java:135) The following method did not exist: 'void org.springframework.util.Assert.notNull(java.lang.Object)' The calling method's class, org.springframework.plugin.core.support.AbstractTypeAwareSupport$BeansOfTypeTargetSource, was loaded from the following location: jar:file:/D:/Maven/respository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar!/org/springframework/plugin/core/support/AbstractTypeAwareSupport$BeansOfTypeTargetSource.class The called method's class, org.springframework.util.Assert, is available from the following locations: jar:file:/D:/Maven/respository/org/springframework/spring-core/6.2.7/spring-core-6.2.7.jar!/org/springframework/util/Assert.class The called method's class hierarchy was loaded from the following locations: org.springframework.util.Assert: file:/D:/Maven/respository/org/springframework/spring-core/6.2.7/spring-core-6.2.7.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.plugin.core.support.AbstractTypeAwareSupport$BeansOfTypeTargetSource and org.springframework.util.Assert Process finished with exit code 0解决办法
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值