春天的AOP@Around访问@annotation的值。

问题描述 投票:2回答:1

我有一个自定义注释为:

@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)

我需要使用 xAudit 内侧,以便进入 行动XAudit. Kindly share some pointers on how can I access the value of @XAudit inside @Around Aspect.

spring annotations aop spring-aop audit
1个回答
1
投票

有一个错别字 @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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.