切面AOP中获取方法的注解相关问题

自己定义一个注解,然后逻辑代码需要通过获取自定义注解的一个属性来进行权限控制。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
    public String value();
}

接口与实现类

public interface UserService {
    
    public void add();
    
    public void del(int id);
    
    public String queryName(String name);

}


@Service("userService")
public class UserServiceImpl implements UserService {

    public void add() {
        System.out.println("执行UserServiceImpl中的add方法.....");
    }

    public void del(int id) {
        System.out.println("执行UserServiceImpl中的del方法.....");

    }

    public String queryName(String name) {
        System.out.println("执行UserServiceImpl中的queryName方法.....");
        return null;
    }
}

方式一:在接口方法上添加注解

public interface UserService {
    @LogAnnotation("执行添加")
    public void add();
    @LogAnnotation("执行删除")
    public void del(int id);
    @LogAnnotation("执行根据名字查询")
    public String queryName(String name);
}

定义一个切面类

@Component
@Aspect
@EnableAspectJAutoProxy
public class LogAspect {
    @Pointcut("execution(* com.spring.service..*.*(..))")
    public void logPoint(){//声明切入点
    }
    //环绕通知
    @Around("logPoint()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws NoSuchMethodException {
        try {
            //获得目标方法的签名对象
            Signature signature = proceedingJoinPoint.getSignature();
            System.out.println(signature.getDeclaringType());
            //将目标方法的签名对象转化为MethodSignature
            MethodSignature methodSignature= (MethodSignature) signature;
            //获得方法的注解
            Method method = methodSignature.getMethod();
            //System.out.println(method);
            Object target = proceedingJoinPoint.getTarget();
            LogAnnotation declaredAnnotation = 
       method.getDeclaredAnnotation(LogAnnotation.class);
            String value = declaredAnnotation.value();
            System.out.println(value);
            proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

方式二:在实现类的方法上添加注解

@Service("userService")
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("执行UserServiceImpl中的add方法.....");
    }

    public void del(int id) {
        System.out.println("执行UserServiceImpl中的del方法.....");

    }

    public String queryName(String name) {
        System.out.println("执行UserServiceImpl中的queryName方法.....");
        return null;
    }
}

切面代码:
 

@Component
@Aspect
@EnableAspectJAutoProxy
public class LogAspect {
    @Pointcut("execution(* com.spring.service..*.*(..))")
    public void logPoint(){//声明切入点
    }
    //环绕通知
    @Around("logPoint()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws NoSuchMethodException {
        try {
            //获得目标方法的签名对象
            Signature signature = proceedingJoinPoint.getSignature();
            //将目标方法的签名对象转化为MethodSignature
            MethodSignature methodSignature= (MethodSignature) signature;
            //获得方法的注解
            Method method = methodSignature.getMethod();
            Object target = proceedingJoinPoint.getTarget();
            Method realMethod = (Method) target .getClass().getDeclaredMethod(signature.getName(),method.getParameterTypes());
LogAnnotation annotation = realMethod.getAnnotation(LogAnnotation.class);
       System.out.println(annotation.value());
            proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

测试代码:

  @Test
public void test(){
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    UserService userService = (UserService) context.getBean("userService");

      userService.add();
      userService.del(10);
      userService.queryName("11");
}

运行结果:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值