Spring AOP @Around access the value of @annotation

匆匆过客 提交于 2020-07-09 07:30:48

问题


I have a custom annotation as,

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

I am using this annotation on some method as following,

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

I am capturing event in aspect as following,

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

The @Around advice works well with only ProceedingJoinPoint argument. If passing XAudit as 2nd argument, it throws following error,

 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)

I need to use xAudit inside aspect in order to access action of XAudit. Kindly share some pointers on how can I access the value of @XAudit inside @Around Aspect.


回答1:


There is a typo in the @Around(value = "@annotation(XAudit)") , it should be xAudit ( x is lower case)

Try : @Around(value = "@annotation(xAudit)")

Also , to get the action value from annotation you need to use xAudit.action() and not xAudit.getAction().

Complete code would be as follows

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


来源:https://stackoverflow.com/questions/62298498/spring-aop-around-access-the-value-of-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!