springframework util

本文介绍了Spring框架中org.springframework.util.Assert类的使用方法,用于方法入参的有效性检查,并展示了Stopwatch工具类的使用,以帮助开发者进行性能测试。

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

一、org.springframework.util.Assert

它断定某一个实际的运行值和预期想一样,否则就抛出异常。Spring 对方法入参的检测借用了这个概念,其提供的Assert 类拥有众多按规则对方法入参进行断言的方法,可以满足大部分方法入参检测的要求。这些断言方法在入参不满足要求时就会抛出
IllegalArgumentException。
demo:
public Boolean exportCrowd(QueryCrowdExport obj) {
		Assert.notNull(obj);
		Assert.notNull(obj.getCrowdId());
		Assert.notNull(obj.getExportType());
		
    	//crowdId获得crowd记录
		Crowd crowd = new Crowd();
		crowd = crowdService.queryCrowdById(obj.getCrowdId());
    	//校验
    	if(ObjectUtils.isEmpty(crowd)==true) {
    		return false;
    	}
    	//添加job、更新标志
    	Boolean res = false;
    	if(obj.getExportType().equals("CC")) {
    		res = jobService.addCCJob(crowd);
    		if(res==true) {
    			crowd.setCcExported(true);
    			crowdMapper.updateByPrimaryKeySelective(crowd);
    		}
    	}else if(obj.getExportType().equals("CIA")) {
    		res = jobService.addCIAJob(crowd,obj);
    		if(res==true) {
    			crowd.setCiaExported(true);
    			crowdMapper.updateByPrimaryKeySelective(crowd);
    		}
    	}	
    	return res;
    }

二、Stopwatch

1、介绍

Stopwatch是一个用于测量代码执行时间的工具。它通常用于性能测试和调试,以确定代码段的运行时间。

2、使用方法
  1. 创建Stopwatch对象:首先,需要创建一个Stopwatch对象。这可以通过调用Stopwatch的构造函数来完成,例如StopWatch stopWatch = new StopWatch("myTest");

  2. 开始计时:使用start()方法开始计时。如果提供了任务名称作为参数,那么计时将与该任务关联,例如stopWatch.start("myTask");

  3. 运行代码:在计时开始后,运行需要测量执行时间的代码。

  4. 停止计时:使用stop()方法停止当前任务的计时。

  5. 获取计时结果:可以通过getTotalTimeMillis()方法获取所有任务的总体执行时间(毫秒单位),或者使用getLastTaskTimeMillis()获取上一个任务的耗时(毫秒单位)。

  6. 打印结果:Stopwatch提供了prettyPrint()方法,可以优美地打印所有任务的详细耗时情况。

3、demo
import org.springframework.util.StopWatch;
 
public class StopWatchTest {
    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch("myTest");
        stopWatch.start("myTask");
        // 模拟一些工作,比如休眠2秒
        Thread.sleep(2000);
        stopWatch.stop();
        System.out.println("Total time for myTask: " + stopWatch.getTotalTimeMillis() + " ms");
        System.out.println("Last task name: " + stopWatch.getLastTaskName());
        System.out.println("Pretty print of tasks: " + stopWatch.prettyPrint());
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值