AOP AspectJ 切点 if() 抛出 ArrayIndexOutOfBoundsException:

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

AOP AspectJ Pointcut if() throwing ArrayIndexOutOfBoundsException:

问题

我正在尝试编写一个切面(Aspect),试图实现Pointcut的if()条件,但是收到了ArrayIndexOutOfBoundsException异常。以下是代码片段。

@Pointcut("call(* com.aop.Service.activate(..)) && args(iActivate,..) && if()")
public static boolean saveActivate(Activate iActivate) {
    return true; // 如果为false,则@Before不会被调用
};

@Before("saveActivate(iActivate)")
public void saveActivateBefore(JoinPoint ijoinPoint, ActivateInstallmentRequest iActivateInstallmentRequest) {
    System.out.println("Log from @before");    
}

这段代码给我抛出了以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: Initialization of bean failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

有人可以帮助我找出我在这里缺少了什么吗?
附注:我还参考了AspectJ文档 1

英文:

I am trying to write an Aspect, trying to implement Pointcut's if() condition but receiving ArrayIndexOutOfBoundsException. here is the code snippet.

@Pointcut("call(* com.aop.Service.activate(..)) && args(iActivate,..) && if()")
	public static boolean saveActivate(Activate iActivate) {
		return true; //if false @before she not be invoked
	};

@Before("saveActivate(iActivate)")
    public void saveActivateBefore(JoinPoint ijoinPoint, ActivateInstallmentRequest iActivateInstallmentRequest) {
        System.out.println("Log from @before");    
}

This code is giving me below exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: Initialization of bean failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

Can Someone help me what I'm missing here?
PS: I have also referred AspectJ.

答案1

得分: 1

经过验证您使用了Spring AOP之后,答案确实是if()call()都不受支持,另请参阅Spring手册关于切入点指示符的部分。

如果您想要使用这些指示符,您需要通过AspectJ通过LTW(加载时织入)来激活。

Spring AOP的一种解决方法是简单地在您的通知方法开头使用常规的if,如果没有特定的原因使用call(),可以使用更常见的execution(),这也是大多数人想要的。我不会在这里详细阐述call和execution连接点之间的区别,但您可以例如参考我在这里的回答获取更多信息。


更新: 您的代码片段中还有其他一些看起来有点奇怪的地方,但我解释的是目前存在的最大问题。

英文:

After having verified that you use Spring AOP, the answer is indeed that both if() and call() are not supported, see also the Spring manual section about pointcut designators.

If you want to use those designators, you need to activate AspectJ via LTW (load-time weaving).

A workaround for Spring AOP would be to simply use a regular if at the beginning of your advice method and, if there are no specific reasons for using call(), use execution() instead, which is what most people want anyway. I am not going to elaborate about the difference between call and execution joinpoints here, but you can e.g. consult my answer there for more information.


Update: There are other things which look a little bit strange in your code snippet, but what I explained is the biggest issue for now.

huangapple
  • 本文由 发表于 2020年10月23日 07:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64491694.html
匿名

发表评论

匿名网友

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

确定