Spring-AOP @AspectJ进阶之绑定连接点方法入参

来源:互联网 发布:怎么在淘宝客上推广 编辑:程序博客网 时间:2024/06/05 23:42

  • 概述
  • 实例

概述

我们前面的博文在讲解切点函数时说过args()、this()、target()、@args()、@within()、@target()和@annotation()这7个函数除了可以指定类名外,还可以指定参数名将目标对象连接点上的方法入参绑定到增强的方法中

其中args()用于绑定连接点方法的入参,@annotation()用于绑定连接点方法的注解对象,而@args()用于绑定连接点方法入参的注解


实例

来看一个args()绑定参数的实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

这里写图片描述

业务类

package com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter;import org.springframework.stereotype.Component;/** *  *  * @ClassName: LogicBindService *  * @Description: @Component标注的Bean *  * @author: Mr.Yang *  * @date: 2017年9月12日 上午1:39:23 */@Componentpublic class LogicBindService {    public void dealLogic(String bussiness, int number) {        System.out.println("deal Logic:" + bussiness + ", number:" + number);    }}

编写切面

package com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;/** *  *  * @ClassName: BindJointPointParameterAspect *  * @Description: @Aspect标注的切面 *  * @author: Mr.Yang *  * @date: 2017年9月12日 上午1:10:40 */@Aspectpublic class BindJointPointParameterAspect {    // ①绑定连接点参数,首先args(name,number,..)根据②处的增强方法入参找到name和number对应的类型,以得到真实的切点表达式:    // target(com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter.LogicBindService)    // && args(String,int,..)    // 在该增强方法织入到目标连接点时,增强方法可以通过num和name访问到连接点方法的入参。    @Before("target(com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter.LogicBindService) && args(name,number,..)")    public void crossCodeCutting(int number, String name) throws Throwable { // ②增强方法接受连接点的参数        System.out.println("----bindJoinPointParams()----");        System.out.println("name:" + name);        System.out.println("number:" + number);        System.out.println("----bindJoinPointParams()----");    }}

在①处,我们通过args(name,number,..)进行连接点参数的绑定,和前面我们所讲述的方式不一样,当args()函数入参为参数名时,共包括两方面的信息:

  • 连接点匹配规则信息:连接点方法第一个入参是String类型,第二个入参是int类型;

  • 连接点方法入参和增强方法入参的绑定信息:连接点方法的第一个入参绑定到增强方法的name参数上,第二个入参绑定到增强方法的number入参上。

切点匹配和参数绑定的过程是这样的:

  • 首先args()根据参数名称在增强方法中查到名称相同的入参并获知对应的类型,这样就知道匹配连接点方法的入参类型。

  • 其次连接点方法入参类型所在的位置则由参数名在args()函数中声明的位置决定。

args(name,number)只匹配第一个入参是String第二个入参是int的目标类方法,如LogicBindService.dealLogic(String bussiness, int number)而不匹配LogicBindService.dealLogic( int number,String bussiness)

切点匹配和参数绑定过程:

这里写图片描述

和args()一样,其它可以绑定连接点参数的切点函数(如@args()和target()等),当指定参数名时,就同时具有匹配切点和绑定参数的双重功能


将业务Bean和切面配置到配置文件中

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件   (2)扫描类包以及应用注解定义的bean --><context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter"/><!-- 基于@AspectJ切面的驱动器 --><aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面类 --><bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter.BindJointPointParameterAspect"/></beans>

测试类

package com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BindJointPointParameterAspectTest {    @Test    public void test() {        ApplicationContext ctx = new ClassPathXmlApplicationContext(                "classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindJointParameter/conf-joinPoint.xml");        LogicBindService logicBindService = ctx.getBean("logicBindService",                LogicBindService.class);        logicBindService.dealLogic("PROGRAMMING", 5);    }}

运行结果

2017-09-12 02:05:35,991  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24177697: startup date [Tue Sep 12 02:05:35 BOT 2017]; root of context hierarchy2017-09-12 02:05:36,098  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindJointParameter/conf-joinPoint.xml]----bindJoinPointParams()----name:PROGRAMMINGnumber:5----bindJoinPointParams()----deal LogicPROGRAMMING, number:5

可见,增强方法按预期绑定了LogicBindService.dealLogic(String bussiness, int number)方法的运行期入参。
提示 为了保证实例能成功执行,必须启用CGLib动态代理:,因为该实例需要对NaiveWaiter类进行代理(因为NaiveWaiter#simle()方法不是Waiter接口的方法),所以必须使用CGLib生成子类的代理方法。

提示 :为了保证实例能成功执行,必须启用CGLib动态代理:<aop:aspectj-autoproxy proxy-target-class="true" />,因为该实例不涉及到接口,所以必须使用CGLib生成子类的代理方法。 当然了,即使不设置(默认为jdk反向代理),当涉及的业务类没有接口时,spring会自动使用cglib代理

原创粉丝点击