Getting error error at ::0 formal unbound in pointcut while accessing annotation value inside an advice using ApectJ

huangapple go评论69阅读模式
英文:

Getting error error at ::0 formal unbound in pointcut while accessing annotation value inside an advice using ApectJ

问题

I'm trying to access custom annotation value inside an advice method but getting an error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:610)
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

Annotation declaration:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Authorize {
    String[] entitlements() default "";
}

This issue is happening with the second argument in the method. If I remove the second argument, the application starts properly.

@Aspect
@Component
public class AspectHandler {
    @Around(value = "@annotation(com.myorg.authorization.annotations.Authorize)")
    public void AuthValidator(ProceedingJoinPoint point, Authorize authorize) throws Throwable {
        String[] entitlements = authorize.entitlements();
        point.proceed();
    }
}

I tried using the argNames argument, but it still doesn't work.

@Around(value = "@annotation(com.virtana.authorization.annotations.Authorize)", argNames = "authorize")

I also tried:

@Around(value = "@annotation(com.virtana.authorization.annotations.Authorize) && args(authorize)")
英文:

I'm trying to access custom annotation value inside an advice method but getting an error

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:610)
Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

Annotation declaration

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Authorize {
    String[] entitlements() default "";
}

This issue happening with second argument in the method, if I removed the second argument application starts properly.

@Aspect
@Component
public class AspectHandler{
    @Around(value = "@annotation(com.myorg.authorization.annotations.Authorize)")
    public void AuthValidator(ProceedingJoinPoint point, Authorize authorize) throws Throwable {
        String[] entitlements = authorize.entitlements();
        point.proceed();
    }
}

I tried with argNames argument, still it doesn't work

@Around(value = "@annotation(com.virtana.authorization.annotations.Authorize)", argNames = "authorize")

also tried

@Around(value = "@annotation(com.virtana.authorization.annotations.Authorize) && args=authorize")

答案1

得分: 0

Your syntax is wrong. For unbound annotations it would be correct to use the fully qualified class name:

@Around(" @annotation(com.myorg.authorization.annotations.Authorize)")
public void validate(ProceedingJoinPoint point) throws Throwable

For an annotation bound to an advice method parameter, you only use the parameter name as a reference:

@Around(" @annotation(authorize)")
public void validate(ProceedingJoinPoint point, Authorize authorize) throws Throwable

By the way: If your around advice returns void, it can only match methods with the same return type. If the target method returns anything else, your advice needs to return the result of proceed() or something other matching the actual return type.

Please also make sure to adhere to Java naming conventions. AuthValidator looks like a class name. Method names should not be nouns like SomeObject but verbal descriptions like doSomething, starting with a lower-case character.

英文:

Your syntax is wrong. For unbound annotations it would be correct to use the fully qualified class name:

@Around("@annotation(com.myorg.authorization.annotations.Authorize)")
public void validate(ProceedingJoinPoint point) throws Throwable

For an annotation bound to an advice method parameter, you only use the parameter name as a reference:

@Around("@annotation(authorize)")
public void validate(ProceedingJoinPoint point, Authorize authorize) throws Throwable

By the way: If your around advice returns void, it can only match methods with the same return type. If the target method returns anything else, your advice needs to return the result of proceed() or something other matching the actual return type.

Please also make sure to adhere to Java naming conventions. AuthValidator looks like a class name. Method names should not be nouns like SomeObject but verbal descriptions like doSomething, starting with a lower-case character.

huangapple
  • 本文由 发表于 2023年4月17日 16:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定