工作忙,时间紧,不过事情再多,学习是必须的。记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习。第一:为了更充实自己,保持进步状态。第二:为了提升技术,提高开发能力。第三:保持程序员对技术和学习的热情,工作的激情。程序员还是需要把基础打扎实,修炼自己的内功。” 所以赶紧把学习的东西总结一下,加深印象。之前有说了下AOP的原理 (http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html) 。基于代理模式,了解了jdk动态代理和cglib的用法。但是在真正的使用AOP的时候,不可能写这么厚重的方法。

Spring有两大核心,IOC和AOP。IOC在java web项目中无时无刻不在使用。然而AOP用的比较少,的确也是一般的项目用的场所不多。事务控制基本都用,但却是Spring封装的不需要我们再去实现,但Spring的AOP远不止这些,不能因为项目中没有使用,而不去学习及理解。我觉得这是作为一个java web软件开发人员必须具备的技能。业内很多将AOP应用在日志记录上,可惜我们项目没这么做,后面需要学习下。在这先把Spring AOP的基本用法,在脑子里理一边,做一次积累。

1、概念术语  

在开始之前,需要理解Spring aop 的一些基本的概念术语(总结的个人理解,并非Spring官方定义):

  • 切面(aspect):用来切插业务方法的类。
  • 连接点(joinpoint):是切面类和业务类的连接点,其实就是封装了业务方法的一些基本属性,作为通知的参数来解析。
  • 通知(advice):在切面类中,声明对业务方法做额外处理的方法。
  • 切入点(pointcut):业务类中指定的方法,作为切面切入的点。其实就是指定某个方法作为切面切的地方。
  • 目标对象(target object):被代理对象。
  • AOP代理(aop proxy):代理对象。

AOP通知类型:

  • 前置通知(before advice):在切入点之前执行。
  • 后置通知(after returning advice):在切入点执行完成后,执行通知。
  • 环绕通知(around advice):包围切入点,调用方法前后完成自定义行为。
  • 异常通知(after throwing advice):在切入点抛出异常后,执行通知。

2、Spring AOP环境

要在项目中使用Spring AOP 则需要在项目中导入除了spring jar包之外,还有aspectjweaver.jar,aopalliance.jar ,asm.jar 和cglib.jar 。

好了,前提工作准备完成,Spring 提供了很多的实现AOP的方式,在学习过程中,循序渐进。进行Spring 接口方式,schema配置方式和注解的三种方式进行学习。好了废话不多说了,开始spring aop学习之旅:

3、方式一:AOP接口

利用Spring AOP接口实现AOP,主要是为了指定自定义通知来供spring AOP机制识别。主要接口:前置通知 MethodBeforeAdvice ,后置通知:AfterReturningAdvice,环绕通知:MethodInterceptor,异常通知:ThrowsAdvice 。见例子代码:

a、业务接口:

/**
 * 代理类接口,也是业务类接口<br>
 * 
 * 利用接口的方式,spring aop 将默认通过jdk 动态代理来实现代理类<br>
 * 不利用接口,则spring aop 将通过cglib 来实现代理类
 * 
 * @author yanbin
 * 
 */
public interface IBaseBusiness {

    /**
     * 用作代理的切入点方法
     * 
     * @param obj
     * @return
     */
    public String delete(String obj);

    /**
     * 这方法不被切面切
     * 
     * @param obj
     * @return
     */
    public String add(String obj);

    /**
     * 这方法切不切呢?可以设置
     * 
     * @param obj
     * @return
     */
    public String modify(String obj);

}

b、业务类:

/**
 * 业务类,也是目标对象
 * 
 * @author yanbin
 * 
 */
public class BaseBusiness implements IBaseBusiness {

    /**
     * 切入点
     */
    public String delete(String obj) {
        System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n");
        return obj + ":瞄~";
    }

    public String add(String obj) {
        System.out.println("================这个方法不能被切。。。============== \n");
        return obj + ":瞄~ 嘿嘿!";
    }

    public String modify(String obj) {
        System.out.println("=================这个也设置加入切吧====================\n");
        return obj + ":瞄改瞄啊!";
    }

}

c、通知类:

前置通知:

/**
 * 前置通知。
 * 
 * @author yanbin
 * 
 */
public class BaseBeforeAdvice implements MethodBeforeAdvice {

    /**
     * method : 切入的方法 <br>
     * args :切入方法的参数 <br>
     * target :目标对象
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("===========进入beforeAdvice()============ \n");

        System.out.print("准备在" + target + "对象上用");
        System.out.print(method + "方法进行对 '");
        System.out.print(args[0] + "'进行删除!\n\n");

        System.out.println("要进入切入点方法了 \n");
    }

}

后置通知:

/**
 * 后置通知
 * 
 * @author yanbin
 * 
 */
public class BaseAfterReturnAdvice implements AfterReturningAdvice {

    /**
     * returnValue :切入点执行完方法的返回值,但不能修改 <br>
     * method :切入点方法 <br>
     * args :切入点方法的参数数组 <br>
     * target :目标对象
     */
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("==========进入afterReturning()=========== \n");
        System.out.println("切入点方法执行完了 \n");

        System.out.print(args[0] + "在");
        System.out.print(target + "对象上被");
        System.out.print(method + "方法删除了");
        System.out.print("只留下:" + returnValue + "\n\n");
    }

}

环绕通知:

/**
 * 环绕通知
 * 
 * @author yanbin
 * 
 */
public class BaseAroundAdvice implements MethodInterceptor {

    /**
     * invocation :连接点
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("===========进入around环绕方法!=========== \n");

        // 调用目标方法之前执行的动作
        System.out.println("调用方法之前: 执行!\n");

        // 调用方法的参数
        Object[] args = invocation.getArguments();
        // 调用的方法
        Method method = invocation.getMethod();
        // 获取目标对象
        Object target = invocation.getThis();
        // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
        Object returnValue = invocation.proceed();

        System.out.println("===========结束进入around环绕方法!=========== \n");

        System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + returnValue + "\n");

        System.out.println("调用方法结束:之后执行!\n");

        return returnValue;
    }

}

异常通知:

/**
 * 异常通知,接口没有包含任何方法。通知方法自定义
 * 
 * @author yanbin
 * 
 */
public class BaseAfterThrowsAdvice implements ThrowsAdvice {

    /**
     * 通知方法,需要按照这种格式书写
     * 
     * @param method
     *            可选:切入的方法
     * @param args
     *            可选:切入的方法的参数
     * @param target
     *            可选:目标对象
     * @param throwable
     *            必填 : 异常子类,出现这个异常类的子类,则会进入这个通知。
     */
    public void afterThrowing(Method method, Object[] args, Object target, Throwable throwable) {
        System.out.println("删除出错啦");
    }

}

d、定义指定切点:

/**
 * 定义一个切点,指定对应方法匹配。来供切面来针对方法进行处理<br>
 * 
 * 继承NameMatchMethodPointcut类,来用方法名匹配
 * 
 * @author yanbin
 * 
 */
public class Pointcut extends NameMatchMethodPointcut {

    private static final long serialVersionUID = 3990456017285944475L;

    @SuppressWarnings("rawtypes")
    @Override
    public boolean matches(Method method, Class targetClass) {
        // 设置单个方法匹配
        this.setMappedName("delete");
        // 设置多个方法匹配
        String[] methods = { "delete", "modify" };

        //也可以用“ * ” 来做匹配符号
        // this.setMappedName("get*");

        this.setMappedNames(methods);

        return super.matches(method, targetClass);
    }

}

e、配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="     
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop     
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    default-autowire="byName">

    <!-- ==============================利用spring自己的aop配置================================ -->
    <!-- 声明一个业务类 -->
    <bean id="baseBusiness" class="aop.base.BaseBusiness" />

    <!-- 声明通知类 -->
    <bean id="baseBefore" class="aop.base.advice.BaseBeforeAdvice" />
    <bean id="baseAfterReturn" class="aop.base.advice.BaseAfterReturnAdvice" />
    <bean id="baseAfterThrows" class="aop.base.advice.BaseAfterThrowsAdvice" />
    <bean id="baseAround" class="aop.base.advice.BaseAroundAdvice" />

    <!-- 指定切点匹配类 -->
    <bean id="pointcut" class="aop.base.pointcut.Pointcut" />

    <!-- 包装通知,指定切点 -->
    <bean id="matchBeforeAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut">
            <ref bean="pointcut" />
        </property>
        <property name="advice">
            <ref bean="baseBefore" />
        </property>
    </bean>

    <!-- 使用ProxyFactoryBean 产生代理对象 -->
    <bean id="businessProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 代理对象所实现的接口 ,如果有接口可以这样设置 -->
        <property name="proxyInterfaces">
            <value>aop.base.IBaseBusiness</value>
        </property>

        <!-- 设置目标对象 -->
        <property name="target">
            <ref local="baseBusiness" />
        </property>
        <!-- 代理对象所使用的拦截器 -->
        <property name="interceptorNames">
            <list>
                <value>matchBeforeAdvisor</value>
                <value>baseAfterReturn</value>
                <value>baseAround</value>
            </list>
        </property>
    </bean>

相关文章:

  • 2021-09-05
  • 2021-08-04
  • 2021-12-30
  • 2022-12-23
  • 2021-04-02
  • 2021-07-08
  • 2021-07-01
  • 2021-06-09
猜你喜欢
  • 2022-12-23
  • 2021-08-23
  • 2021-10-06
  • 2021-11-01
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案