分布式编程-实现分布式锁-优雅的使用自定义注解实现
package com.beauty.beautybase.AOP;
import com.beauty.beautybase.annotion.DistributeLock;
import com.beauty.beautybase.annotion.LockType;
import com.beauty.beautybase.service.RedissonService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* description 分布式锁自定义注解 切面
*
* @author yufw
* date 2020/4/27 13:11
*/
@Aspect
@Component
public class DistributeLockAop {
@Autowired
RedissonService redissonService;
/**
* 切入点
*/
@Pointcut("@annotation()")
public void doBusiness() {
}
/**
* 前事件
*
* @param joinPoint
*/
@Before("doBusiness()")
public void doBefore(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取执行方法
Method method = signature.getMethod();
// 获取注解信息
DistributeLock distributeLock = method.getAnnotation(DistributeLock.class);
if (null != distributeLock) {
//获取注解参数值
String lockName = distributeLock.name();
LockType type = distributeLock.type();
if (type == LockType.LOCK) {
redissonService.lock(lockName);
System.out.println("加锁成功");
}
}
}
/**
* 后事件
*
* @param joinPoint
*/
@After("doBusiness()")
public void doAfter(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取执行方法
Method method = signature.getMethod();
// 获取注解信息
DistributeLock distributeLock = method.getAnnotation(DistributeLock.class);
//获取执行方法名
String methodName = method.getName();
//获取方法传递的参数
Object[] args = joinPoint.getArgs();
if (null != distributeLock) {
//获取注解参数值
String lockName = distributeLock.name();
if (null != distributeLock) {
redissonService.unlock(lockName);
System.out.println("解锁成功");
}
}
}
}