首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring AOP @AOP访问@批注的值

Spring AOP @AOP访问@批注的值
EN

Stack Overflow用户
提问于 2020-06-10 07:56:34
回答 1查看 1.1K关注 0票数 2

我有一个自定义注释,

代码语言:javascript
复制
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XAudit {
    AuditActionType action();
}

我在一些方法上使用这个注释,如下所示,

代码语言:javascript
复制
@XAudit(action = "REGISTRATION")
public RegistrationDTO register(UserDetail detail) {
    //Logic
    return dto;
}

我捕捉事件的方面如下,

代码语言:javascript
复制
@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作为第二个参数传递,则会引发以下错误:

代码语言:javascript
复制
 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 in方面来访问XAuditaction。请分享一些关于如何访问“XAudinin@”的值的提示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-10 08:41:24

@Around(value = "@annotation(XAudit)")中有一个错误,它应该是xAudit (x是小写)

尝试:@Around(value = "@annotation(xAudit)")

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

完整的代码如下所示

代码语言:javascript
复制
@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;
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62298498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档