AOP实现执行时间获取

@Component
@Aspect//切面注解
public class RunTimeAOP {
    //所有的业务方式service
    @Around(value = "execution(* com.jt.service..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) {
        Long startTime = System.currentTimeMillis();
        Object obj = null;
        try {
            obj = joinPoint.proceed();
            Long endTime = System.currentTimeMillis();
            Long runTime = endTime - startTime;
            String targetName = joinPoint.getSignature().getDeclaringTypeName();
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
            String kind = joinPoint.getKind();
            Object target = joinPoint.getTarget();
            System.out.println("类名:" + targetName);
            System.out.println("方法名:" + methodName);
            System.out.println("参数信息:" + args);
            System.out.println("kind:" + kind);
            System.out.println("目标对象:" + target);
            System.out.println("执行时间:" + runTime);
        } catch (Throwable e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return obj;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Spring AOP的Aspect注解来实现操作记录到数据库的功能。首先,需要创建一个切面类,使用@Aspect注解进行标记,并在该类中声明一个@Before注解的方法。在该方法中,使用JoinPoint对象来获取连接点信息,进而获取操作记录的相关信息,并将其保存到数据库中。需要注意的是,操作记录的相关信息应该尽可能地详细,包括操作人员、操作时间、操作类型、操作目标等。 示例代码如下: @Aspect @Component public class OperatorAspect { @Autowired private OperatorLogService operatorLogService; @Before("execution(* com.example.demo.service.*.*(..))") public void logOperator(JoinPoint joinPoint) { OperatorLog operatorLog = new OperatorLog(); operatorLog.setOperatorName("Admin"); operatorLog.setOperatorTime(LocalDateTime.now()); operatorLog.setOperatorType("Update"); operatorLog.setOperatorTarget(joinPoint.getSignature().toShortString()); operatorLogService.save(operatorLog); } } 其中,JoinPoint对象提供了获取连接点信息的多种方法,如下表所示: 方法名 描述 getArgs() 获取连接点方法运行时的参数列表 getThis() 获取代理对象 getTarget() 获取目标对象 getSignature() 获取连接点的方法签名对象 toShortString() 获取连接点的方法简短签名名称 toLongString() 获取连接点的方法全限定签名名称 getKind() 获取连接点类型,如method-execution getSourceLocation() 获取连接点所在的位置 getStaticPart() 获取连接点所在的静态部分 需要在Spring配置文件中将该切面类注入到容器中,并启用AOP自动代理: <context:component-scan base-package="com.example.demo"/> <aop:aspectj-autoproxy proxy-target-class="true"/>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值