反射 + AOP - 通过调用调用方法时未拦截建议
Reflection + AOP - advice not intercepted when calling method via 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); }
但是:当通过 "invoke".
调用方法时,环绕通知永远不会被拦截
Around advice 在被diodeService.blink()
调用时正常拦截
拜托,有什么想法吗?
Spring AOP 基于运行时编织,因为Spring 的基于代理的性质 ] 框架。这意味着目标 bean 是一个 Spring-managed bean。在 Spring 运行时,bean 变成了 proxy。
如果 DiodeService
是 Spring 代理 (由 Spring 管理),Spring AOP将按预期正常工作,即对于任何对 blink
.
的调用,建议 将被拦截
如果DiodeService
直接而不是通过[实例化,blink
的建议将永远不会被拦截 =39=].当您通过调用 newInstance
、
实例化 DiodeService
时就是这种情况
Object t = c.newInstance()
通过反射:
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); }
但是:当通过 "invoke".
调用方法时,环绕通知永远不会被拦截Around advice 在被diodeService.blink()
拜托,有什么想法吗?
Spring AOP 基于运行时编织,因为Spring 的基于代理的性质 ] 框架。这意味着目标 bean 是一个 Spring-managed bean。在 Spring 运行时,bean 变成了 proxy。
如果 DiodeService
是 Spring 代理 (由 Spring 管理),Spring AOP将按预期正常工作,即对于任何对 blink
.
如果DiodeService
直接而不是通过[实例化,blink
的建议将永远不会被拦截 =39=].当您通过调用 newInstance
、
DiodeService
时就是这种情况
Object t = c.newInstance()