反射加SpringAPO的综合用法"/>
JoinPoint加反射加SpringAPO的综合用法
// 前置通知(Before advice)- 在目标方便调用前执行通知// 后置通知(After advice)- 在目标方法完成后执行通知// 返回通知(After returning advice)- 在目标方法执行成功后,调用通知// 异常通知(After throwing advice)- 在目标方法抛出异常后,执行通知// 环绕通知(Around advice)- 在目标方法调用前后均可执行自定义逻辑@Before("@annotation(timeAno)")//我自己定义的注解可以绑定到任何方法是public synchronized void beforeRequestFlat(JoinPoint joinPoint, TimeAno timeAno) throws Exception { // Object getTarget(); 获取被代理的对象 // Object getThis(); 获取代理对象Object[] args = joinPoint.getArgs();//获取目标方法的参数值for (int i = 0; i < args.length; i++) {System.out.println("第" + (i + 1) + "个参数为:" + args[i]);}System.out.println(joinPoint.getTarget().getClass().getName());Class<?> clazz =joinPoint.getTarget().getClass();try {Field f = clazz.getDeclaredField("result");//f.setAccessible(true);try {f.set(clazz.newInstance(),"18");} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();}} catch (NoSuchFieldException e) {e.printStackTrace();}
}
更多推荐
JoinPoint加反射加SpringAPO的综合用法
发布评论