问题描述
我有一个自定义注释,
@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;
}
@Around 建议仅适用于 ProceedingJoinPoint 参数.如果将 XAudit 作为第二个参数传递,则会引发以下错误,
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)
我需要在方面使用 xAudit 才能访问 XAudit 的 action.请分享一些有关如何访问 @Around Aspect 中 @XAudit 值的指针.
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.
推荐答案
@Around(value = "@annotation(XAudit)")
中有一个错字,应该是 xAudit
(x 为小写)
There is a typo in the @Around(value = "@annotation(XAudit)")
, it should be xAudit
( x is lower case)
尝试:@Around(value = "@annotation(xAudit)")
此外,要从注释中获取操作值,您需要使用 xAudit.action()
而不是 xAudit.getAction()
.
Also , to get the action value from annotation you need to use xAudit.action()
and not 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 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!