Spring AOP @Around 访问@annotation 的值

标签 spring annotations aop spring-aop audit

我有一个自定义注释,

@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 建议仅适用于 ProceedingJoinPoint 争论。如果通过 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)

我需要使用 xAudit 内部方面以访问 行动 XAudit .请分享一些有关如何访问@Around Aspect 中@XAudit 值的指针。

最佳答案

@Around(value = "@annotation(XAudit)") 中有错别字,应该是 xAudit ( x 是小写)

试试:@Around(value = "@annotation(xAudit)")
此外,要从注释中获取操作值,您需要使用 xAudit.action()而不是 xAudit.getAction() .

完整的代码如下

@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;
    }
}

关于Spring AOP @Around 访问@annotation 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62298498/

相关文章:

java - 12 因素应用程序 - 依赖泄漏

Spring 启动 : Enable HTTPS for embedded tomcat

java - Spring Security - 为什么要为用户和角色设置单独的类?

java - hibernate/jpa如何存储一个实体之间的关系

android - 更新到 AndroidStudio 3.2,尝试构建时出现问题 : Could not find the AndroidManifest. xml 文件,使用生成文件夹

java - 一个注释怎么可能是它自己的注释呢?

java - 字节的 Spring REST 模板

java - 如何定义哪些方面应该融入到我的项目中,哪些方面不应该融入到 AspectJ 中?

aop - 如何覆盖某些类的 Class<?>.getName() ?

c++ - Objective-C 中的 AOP : Inject context-aware code into each method while maintaining DRY