当前位置: 首页 > 知识库问答 >
问题:

SpringAOP@Around访问@annotation的值

充阳秋
2023-03-14

我有一个自定义注释,

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XAudit {
    AuditActionType action();
}

我正在将这个注释用于以下方法,

@XAudit(action = "REGISTRATION")
public RegistrationDTO register(UserDetail detail) {
    //Logic
    return dto;
}

我在以下方面捕捉事件,

@Around(value = "@annotation(XAudit)")
public Object postAuditEvent(ProceedingJoinPoint joinPoint, XAudit xAudit) throws Throwable {

        String action = xAudit.action();  //Accessing Action for logic decision making  

        Object result = joinPoint.proceed();

        //audit processing logic

        return result;
}

@Around建议仅适用于“进程连接点”参数。如果将 XAudit 作为第二个参数传递,则会引发以下错误:

 Error creating bean with name 'metaDataSourceAdvisor': Cannot resolve reference to bean 'methodSecurityMetadataSource' while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration': Unsatisfied dependency expressed through method 'setObjectPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: 
error at ::0 formal unbound in pointcut 
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
            at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
            at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:238)
            at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:710)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535)
            at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
            at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
            at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
            at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)

我需要在方面中使用xaud才能访问Xaud的操作。请分享一些关于如何在@周围方面中访问@Xaud值的指针。

共有1个答案

裴英锐
2023-03-14

@Around(值 = “@annotation(XAudit)”)中有一个拼写错误,它应该是 xAudit(x 是小写的)

试试:< code > @ Around(value = " @ annotation(x audit)")

此外,要从注释中获取操作值,需要使用xAudit。action()而不是xAudit.getAction()

完整的代码如下

java prettyprint-override">@Component
@Aspect
public class XAuditAspect {
    @Around(value = "@annotation(xAudit)")
    public Object postAuditEvent(ProceedingJoinPoint joinPoint, XAudit xAudit) throws Throwable {
            String action = xAudit.action();  //Accessing Action for logic decision making
            Object result = joinPoint.proceed();
            //audit processing logic
            System.out.println(xAudit.action());
            return result;
    }
}
 类似资料:
  • around 是一个权限管理解决方案,它基于 springboot 实现,遵循阿里 java 开发手册,代码实现相对于 springmvc 来说更加优雅。缓存可以不基于任何缓存复杂臃肿的缓存系统,但是简单的实现了自带的 redis 缓存。在配置里可以关闭,也可以自定义任何缓存系统,来实现你心目中的样子。权限模块是实现了 springframework 的 HandlerInterceptor 这个

  • 主要内容:读者,前提条件,Spring AOP 概述Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。 读者 本教程主要是为Spring 面向方面编程(AOP)初学者准备的,帮助他们了解与Spring的AOP框架相关的基础到高级概念。 前提条件 在开始练习本教程系列文章中给出的各种类型的示例之前,我们假设您已经了解

  • 注释是Tapestry利用的一个非常重要的特性,用于简化Web应用程序开发。 Tapestry提供了许多自定义注释。 它具有类,方法和成员字段的注释。 如前一节所述,Annotation也可用于覆盖功能的默认约定。 Tapestry注释分为四个主要类别,它们如下所示。 组件注释 用于Pages,Components和Mixins类。 一些有用的注释是 - @Property - 适用于领域。 用于

  • @Around是一种通知类型,可确保在方法执行之前和之后运行建议。 以下是@Around建议的语法。 语法 (Syntax) @PointCut("execution(* com.wenjiangs.Student.getAge(..))") private void selectGetName(){} @Around("selectGetAge()") public void aroundAdv

  • Around是一种通知类型,可确保在方法执行之前和之后运行建议。 以下是around建议的语法。 语法 (Syntax) <aop:config> <aop:aspect id = "log" ref = "logging"> <aop:PointCut id = "PointCut-id" expression = "execution( expression )"/>

  • Weather Around (YQL Demo) 应用是用来展示如何使用 iOS 的 YQL ,解析和显示 JSON 格式的数据。同时,它还包括一些简单的 GPS 位置示例。用户可以输入多个城市的名字,通过 SQL(比如 YQL API 的查询信息)来获取天气预报信息。