Spring AOP 和 拦截器 获取类上与方法上的注解

在做一个跨过目标注解的鉴权功能时,想到了AOP与拦截器两种方式,其中 @HasPermission 是我自定义的注解,以下分别为AOP与拦截器获取访问目标类与方法上的注解的方法。由于我的系统在拦截器上配置了拦截过则,所以我选的是拦截器的方式,读者可根据自己的需求来。

一、Spring AOP

先通过ProceedingJoinPoint对象的 joinPoint.getSignature()方法获取到 Signature 的对象并强制类型转换为一个MethodSignature对象,通过 signature.getClass()方法获取到一个 Class 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标类上的目标注解;
同理,通过 signature.getMethod()方法获取到一个 Method 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标方法上的目标注解。

@Aspect
@Component
public class PermissionAop {

    @Pointcut("execution(public * com.xxx.yyy.controller.*.*(..))")
    public void pointcut(){

    }

    @Around("pointcut()")
    public ApiResult doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //1.获取目标类上的目标注解(可判断目标类是否存在该注解)
        HasPermission annotationInClass = AnnotationUtils.findAnnotation(signature.getClass(), HasPermission.class);
        //2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
        HasPermission annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), HasPermission.class);
        //ps:如果1.无法获取类上的注解时
        //使用反射的方式
        /* 
         * MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         * Class<?> tagClass = signatureInMethod.getDeclaringType();
         * boolean annotation = tagClass.isAnnotationPresent(HasPermission.class);
         * HasPermission annotationInClass=null;
         * if(annotation){
         *     annotationInClass = tagClass.getAnnotation(HasPermission.class);
         * }
         * 
         */
        
        //....
        //具体业务逻辑
        //....
        
        return (ApiResult) joinPoint.proceed(args);
    }

}

二、拦截器  

handler 强制转换为 HandlerMetho 对象,再通过 HandlerMethod 对象的handlerMethod.getBeanType() 方法获取到 Bean 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标类上的目标注解;
同理,通过handlerMethod.getMethod() 方法获取到 Method 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标方法上的目标注解。

@Component
public class PermissionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
	    HandlerMethod handlerMethod = (HandlerMethod) handler;
        //1.获取目标类上的目标注解(可判断目标类是否存在该注解)
        HasPermission annotationInClass = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), HasPermission.class);
        //2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
        HasPermission annotationInMethod = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), HasPermission.class);

    	//....
    	//..业务逻辑代码
    	//..
  }
}

PyTorch是一个开源的深度学习框架,它提供了很多用于构建和训练神经网络的工具和函数。对于使用40显卡进行深度学习训练,你可以使用PyTorch来利用GPU进行加速。 首先,确保你已经安装了适合你的显卡的驱动程序。然后,安装PyTorchCUDA工具包,以便与GPU配合使用。你可以根据自己的需求选择合适的PyTorch版本,例如安装PyTorch 1.8.0版本: ```shell pip install torch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 ``` 接下来,安装CUDA工具包。你需要根据你的显卡型号和操作统选择合适的CUDA版本,并根据官方文档的指引进行安装。 安装完成后,你可以在你的PyTorch代码中使用如下代码来检查是否成功地使用GPU进行加速: ```python import torch if torch.cuda.is_available(): device = torch.device("cuda") # 使用GPU加速 print(f"PyTorch version: {torch.__version__}") print(f"CUDA version: {torch.version.cuda}") print(f"Device: {torch.cuda.get_device_name(0)}") else: device = torch.device("cpu") # 使用CPU print("No GPU available, using CPU instead.") ``` 这段代码会输出PyTorch版本、CUDA版本和设备信息,如果成功输出GPU相关信息,则表示你已经成功配置好了GPU加速。 在使用PyTorch进行训练时,你可以将模型和数据移动到GPU上,以便利用GPU的并行计算能力: ```python model.to(device) # 将模型移动到GPU上 inputs, labels = inputs.to(device), labels.to(device) # 将数据移动到GPU上 ``` 这样,你就可以利用40显卡的强大性能来加速训练过程了。希望这些信息对你有帮助!如果还有其他问题,请随时提问。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值