关于aop切面 注解、参数如何获取

目录
  • aop切面 注解、参数如何获取
    • 定义需要切面的注解
    • 在需要进行切面的方法标注注解
    • 定义切面
  • aop中获取自定义注解的属性值
    • 自定义注解
    • 用在方法上
    • 获取注解的属性值

aop切面 注解、参数如何获取

在工作中会经常使用aop,这里将aop使用基本方法,获取在切点中使用的获取参数、注解做一个样例。

定义需要切面的注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnnDemo {
    String value();
    boolean isAop() default true;
}

在需要进行切面的方法标注注解

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/all")
    @AnnDemo(value = "all",isAop = false)
    public List<TbOrder> findAll() {
        List<TbOrder> list = orderService.getOrderList();
        return list;
    }
    @RequestMapping("/page")
    @AnnDemo(value = "page")
    public List<TbOrder> findPage(@RequestParam("username") String username) {
        List<TbOrder> listPage = orderService.getOrdersListPage();
        return listPage;
    }
}

定义切面

在切面中获取切点注解,方法,参数的获取

@Aspect
@Component
public class AspectDemo {
    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))")
    public void excetionMethod() {}
    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
    public void excetionNote() { }
    @Before("excetionMethod()")
    public void testBefore(JoinPoint joinPoint) {
        System.out.println("----------------------------前置通知---");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
    }
    @Around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
    public Object  testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {
        //用的最多通知的签名
        Signature signature = joinPoint.getSignature();
        MethodSignature msg=(MethodSignature) signature;
        Object target = joinPoint.getTarget();
        //获取注解标注的方法
        Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());
        //通过方法获取注解
        AnnDemo annotation = method.getAnnotation(AnnDemo.class);
        Object proceed;
        //获取参数
        Object[] args = joinPoint.getArgs();
        System.out.println(annotation.value());
        System.out.println(annotation.isAop());
        for (Object arg : args) {
            System.out.println(arg);
        }
        if (Objects.isNull(annotation) || !annotation.isAop()) {
            System.out.println("无需处理");
            proceed = joinPoint.proceed();
        }else {
            System.out.println("进入aop判断");
            proceed = joinPoint.proceed();
            if(proceed instanceof List){
                List proceedLst = (List) proceed;
                if(!CollectionUtils.isEmpty(proceedLst)){
                    TbOrder tbOrder = new TbOrder();
                    tbOrder.setPaymentType("fffffffffffffffffff");
                    ArrayList<TbOrder> tbOrderLst = new ArrayList<>();
                    tbOrderLst.add(tbOrder);
                    return tbOrderLst;
                }
            }
            System.out.println(proceed);
        }
        return proceed;
    }
}

aop中获取自定义注解的属性值

自定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemLog {
 
    public String description() default "";
}

用在方法上

@ResponseBody
@ValidRequestBody
@RequestMapping("/login")
@SystemLog(description="登录")
public GlobalResponse login(@RequestBody @Valid User user, BindingResult bindingResult){
    ......
}

获取注解的属性值

@Around("@annotation(com.xxx.xxx.xxx.SystemLog)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
    SystemLog systemLog = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(SystemLog.class);    
    ......
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Spring AOP 切面@Around注解的用法说明

    @Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务. 比如我们想在执行controller中方法前打印出请求参数,并在方法执行结束后来打印出响应值,这个时候,我们就可以借助于@Around注解来实现: 再比如我们想在执行方法时动态修改参数值等 类似功能的注解还有@Before等等,用到了Spring AOP切面思想,Spring AOP常用于拦截器.事务.日志.权限验证等方面. 完整演示代码如下: 需要说明的是,在以下例子中,我们即可以只用@Around注解,并设置条件,

  • Spring AOP使用@Aspect注解 面向切面实现日志横切的操作

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型. 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率. 在Spring AOP中业务逻辑仅仅只关注业务本身,将日志记录,性能统计,安全控制,事务处理,异

  • Spring使用AspectJ的注解式实现AOP面向切面编程

    1.认识Spring AOP 1.1 AOP的简介 AOP:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,而且类的继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足. 1.2 AOP中的概念 切入点(pointcut): 切入点(pointcut):在哪些类.哪些方法上切入. 通知(advice):在方法前.方法后.方法前后做什么. 切面(aspe

  • SpringMVC记录我遇到的坑_AOP注解无效,切面不执行的解决

    AOP注解无效,切面不执行的解决 想做一个api请求日志,想到使用aop,配置过程中遇到了一个坑,aop不起作用, 我的aop是这样的: package com.ljwm.ibei.aspact; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.l

  • 关于aop切面 注解、参数如何获取

    目录 aop切面 注解.参数如何获取 定义需要切面的注解 在需要进行切面的方法标注注解 定义切面 aop中获取自定义注解的属性值 自定义注解 用在方法上 获取注解的属性值 aop切面 注解.参数如何获取 在工作中会经常使用aop,这里将aop使用基本方法,获取在切点中使用的获取参数.注解做一个样例. 定义需要切面的注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @inter

  • springboot使用自定义注解实现aop切面日志

    平时我们在开发过程中,代码出现bug时为了更好的在服务器日志中寻找问题根源,会在接口的首尾打印日志,看下参数和返回值是否有问题.但是手动的logger.info() 去编写时工作量较大,这时我们可以使用AOP切面,为所有接口的首尾打印日志. 实现AOP切面日志一般有两种方式: 1.拦截所有接口controller,在首尾打印日志2.拦截指定注解的接口,为有该注解的接口首尾打印日志 我们尝试用自定义注解来实现AOP日志的打印,这样拥有更高的灵活性.废话不多说,我们开始 1. 导入切面需要的依赖包

  • SpringBoot自定义注解之实现AOP切面日志详解

    通过自定义注解的方式(如:@SysLog(obj = "操作对象", text = "操作内容"),在 SpringBoot 中来实现 AOP 切面统一打印出入参日志. 一.先看下项目结构 二.Maven JAR依赖 <!-- AOP -->     <dependency>             <groupId>org.springframework.boot</groupId>             <

  • Spring AOP 基于注解详解及实例代码

    Spring AOP  基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: <beans xmlns:aop="http://www.springframework.org/schema/aop"...> <!--启动支持--> <aop:aspectj-autoproxy /> </beans> 也可以配置AnnotationAwareAspectJAutoProxyCreator Bean来启动Spring对@

  • spring AOP自定义注解方式实现日志管理的实例讲解

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven /> <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 --> <context:component-scan base-package=&qu

  • springboot配置aop切面日志打印过程解析

    这篇文章主要介绍了springboot配置aop切面日志打印过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.SpringBoot Aop说明 1. Aop AOP(Aspect-Oriented Programming,面向切面编程),它利用一种"横切"的技术,将那些多个类的共同行为封装到一个可重用的模块.便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性. 2. AOP相关概念: Aspect

  • Proxy实现AOP切面编程案例

    通过JDK的Proxy代理实现对业务类做简单的AOP实现 接口:UserService 包含的方法为切入点,会被代理拦截 类:UserServiceImpl 实现UserService接口 类:UserServiceFactory 工厂模式生成动态代理 类:MyAspect 切面类,实现对切入点的操作 UserService public interface UserService { //切面: 需要被拦截的方法 public void addUser(); public void updat

  • 手写redis@Cacheable注解 参数java对象作为key值详解

    目录 1.实现方式说明 1.1问题说明 1.2实现步骤 2.源代码 3.测试 1.实现方式说明 本文在---- 手写redis @ Cacheable注解支持过期时间设置   的基础之上进行扩展. 1.1问题说明 @ Cacheable(key = “'leader'+#p0 +#p1 +#p2” )一般用法,#p0表示方法的第一个参数,#p1表示第二个参数,以此类推. 目前方法的第一个参数为Java的对象,但是原注解只支持Java的的基本数据类型. 1.2实现步骤 1.在原注解中加入新的参数,

  • Spring AOP  基于注解详解及实例代码

    Spring AOP  基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: <beans xmlns:aop="http://www.springframework.org/schema/aop"...> <!--启动支持--> <aop:aspectj-autoproxy /> </beans> 也可以配置AnnotationAwareAspectJAutoProxyCreator Bean来启动Spring对@

随机推荐