浅谈Spring AOP中args()和argNames的含义
1、切入点表达式部分如果增加了args()部分,那么目标方法除了要满足execution部分,还要满足args()对方法参数的要求,对于符合execution表达式,但不符合args参数的方法,不会被植入切面。
2、定义了args()之后,才能把目标方法的参数传入到切面方法的参数中(通过Joinpoint也可以获取参数,但当前方法是直接用切面方法参数接受)。
示例1目标方法:
@RestController@RequestMapping('/testAop')public class TestController { private Logger logger = LoggerFactory.getLogger(TestController.class); @RequestMapping('/helloworld') public String helloWorld(String id, Integer age){System.out.println('被代理方法正在执行');return null; }}
切面方法
@After('execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)') public void after(JoinPoint point, String userId, Integer userAge){System.out.println('userId===========' + userId);System.out.println('userAge===========' + userAge); }
输出结果:
被代理方法正在执行userId===========bian1996userAge===========24
定义了args(userId, userAge)才能把目标方法helloWorld(String id, Integer age)的参数传入到增强处理方法after的参数中,id参数对应userId,age参数对应userAge。使用的方法是按顺序一一对应,helloWorld第一个参数对args第一个参数,helloWorld第2个参数对args第2个参数。
切入点表达式部分增加了&&args(userId, userAge)部分,意味着可以在增强处理方法中定义userId、userAge两个形参------定义这两个形参时,形参类型可以随意指定,但是一旦指定,譬如这里分别是String类型和Integer类型,这两个形参类型将用于限制该切入点只匹配第一个参数类型为String,第二个参数类型为Integer的方法。
也就是,args()中的参数会和目标方法的参数除了在顺序上一一对应之外,在类型上也要对应,否则匹配失败,如下两种情况都会匹配失败。
@RequestMapping('/helloworld')public String helloWorld(Integer id, Integer age){ System.out.println('被代理方法正在执行'); return null;} @After('execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)')public void after(JoinPoint point, String userId, String userAge){ System.out.println('userId===========' + userId); System.out.println('userAge===========' + userAge);}@RequestMapping('/helloworld')public String helloWorld(Integer sex, String id, Integer age){ System.out.println('被代理方法正在执行'); return null;} @After('execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)') public void after(JoinPoint point, String userId, Integer userAge){ System.out.println('userId===========' + userId); System.out.println('userAge===========' + userAge); }
除此之外,使用args()表达式时还可使用如下形式:args(userId, userAge,..),这表明增强处理方法中可以通过userId, userAge来访问目标方法的参数。注意上面args表达式括号中的2点,它表示可以匹配更多参数,但是只要前两个userId, userAge参数匹配上了,目标方法就可以被匹配上。
argNames是可选的,如果没有argNames这个参数,而编译器设置了【在class文件生成变量调试信息】,则spring可以通过反射知道方法参数的名字,通过名字配对,Spring知道args(userId, userAge)表达式里面的userId和userAge,对应了增强方法public void after(JoinPoint point, String userId, Integer userAge)方法里面的userId和userAge,就是第一个示例的情况:
总结:目标方法和args()通过参数顺序一一进行匹配
args()和增强方法通过参数名称一致进行匹配。
但是,如果设置了argNames,Spring不再使用方法参数的名字来配对,使用argNames定义的顺序来给
after(JoinPoint point, String userAge, String userId)的参数传值,例如:argNames='userId,userAge',userId在userAge前面,表示after方法第一个参数(JoinPoint 除外)是userId,第二个参数是userAge,示例如下:
目标方法
@RequestMapping('/helloworld')public String helloWorld(String id, String age){ System.out.println('被代理方法正在执行'); return null;}
切面方法
@After(value = 'execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)', argNames = 'userId,userAge')public void after(JoinPoint point, String userAge, String userId){ System.out.println('userId===========' + userId); System.out.println('userAge===========' + userAge);}
请求连接和输出结果
请求连接http://localhost:8088/testAop/helloworld?age=24&id=bian1996输出结果被代理方法正在执行userId===========24userAge===========bian1996注意:这一次两个参数的类型都给成String类型了
总结:目标方法和args()通过参数顺序一一进行匹配
args()和argNames通过参数名称一致进行匹配
argNames和增强方法通过参数顺序一一对应。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章: