Reflection + AOP - advice not intercepted when calling method via invoke(反射+AOP-通过Invoke调用方法时未截获通知)
问题描述
通过反射:
Class c = Class.forName("mypackage.DiodeService");
Method m = c.getDeclaredMethod("blink");
Object t = c.newInstance();
Object o = m.invoke(t);
调用DiodeService中的方法:
@ValueGreaterThan
public void blink(){
log.info("Diode service: blink()");
}
在注释@ValueGreaterThan为侦听方面:
@Around(value = "mypackage.CommonJoinPointConfig.valueGreaterThanAspect())") public void around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("Value greater than aspect {}", joinPoint); }
BUT:当通过"Invoke"调用方法时,从不截取周围的通知。
当diodeService.blink()
调用时,正常截取周围的建议
请注意,有什么想法吗?
推荐答案
Spring AOP基于运行时编织,这是因为基于代理的性质。这意味着目标Bean是一个由Spring管理的Bean。在Spring运行时,该Bean被转换为代理。
如果DiodeService
是Spring代理(由Spring管理),则Spring AOP将正常工作,即对blink
的任何调用都将截获通知。
如果DiodeService
直接实例化而不是通过Spring实例化,blink
周围的通知将永远不会被截取。这是通过调用newInstance
,
DiodeService
时的情况
Object t = c.newInstance()
这篇关于反射+AOP-通过Invoke调用方法时未截获通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!