美文网首页
Spring(1) - AOP 编程

Spring(1) - AOP 编程

作者: 谷鸽不爱吃稻谷 | 来源:发表于2017-01-02 08:08 被阅读14次

AOP基础 - 动态代理

1). 创建一个接口:Counter.java

public interface Counter {
    public int add(int a, int b);
    public int sub(int a, int b);
}

2). 创建这个接口的实现类:CounterImp.java

public class CounterImp implements Counter {
    
    public int add(int a, int b){
        //加法核心代码
        return a + b;
    }

    public int sub(int a, int b){
        //减法核心代码
        return a - b;
    }
}

3). 创建一个代理类:CounterProxy.java

public class CounterProxy {

    //要代理的对象
    private CounterProxy target;
    
    //构造器,用于给 target 赋值
    public CounterProxy(Counter target){
        this.target = target;
    }
    
    //获取代理
    public Counter getProxy(){
        Counter proxy = null;
        
        //代理对象由哪一个类加载器负责加载
        ClassLoader loader = target.getClass().getClassLoader();
        //代理对象的类型,即其中有哪些方法
        Class [] interfaces = new Class[]{Counter.class};
        //当调用代理对象其中的方法时,该执行的代码
        InvocationHandler h = new InvocationHandler(){
            /**
             * proxy: 正在返回的那个代理对象,一般情况下,在 invoke 方法中都不使用该对象,会无限循环
             * method: 正在被调用的方法
             * args: 调用方法时,传入的参数
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwble{
                String methodName = method.getName();
                //日志
                System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
                //执行方法
                Object result = method.invoke(target, args);
                //日志
                System.out.println("The method " + methodName + " ends with " + result);
                return result;
            }
        }
        
        proxy = (Counter) Proxy.newProxyInstance(loader, interfaces, h);
        return proxy;
    }

}

4). 创建一个测试类:Main.java

public class Main{
    public static void main(String[] args){
        
        Counter target = new CounterImp();
        Counter proxy = new CounterProxy(target).getProxy();
        
        int result = proxy.add(1, 2);
        System.out.println("-->" + result);
        
        result = proxy.sub(2, 1);
        System.out.println("-->" + result);
        
    }
}

5). 运行结果:

The method add begins with [1, 2]
The method add ends with 3
-->3
The method sub begins with [2, 1]
The method sub ends with 1
-->1

Spring AOP

1. 通知

Spring 的 AOP 包括六种通知类型:前置通知,后置通知,返回通知,异常通知i,环绕通知,以及引用通知(不常用)。

  • 前置通知:在方法执行前
  • 后置通知:在方法执行后,不管有没有异常
  • 返回通知:在方法正常结束后
  • 异常通知:在方法抛出异常时
  • 环绕通知:类似动态代理全过程,可以自定义切面位置

2.配置方式

1). 注解方式:略

2). 基于 Spring 配置文件:applicationContext.xml
i. 创建一个 日志切面:LogginAspect.java

public class LoggingAspect{
    
    //前置通知
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = jointPoint.getSinature().getName();
        Object [] args = jointPoint.getArgs();
        
        System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
    }
    
    //返回通知
    public void afterReturning(JoinPoint joinPoint, Object result){
        String methodName = jointPoint.getSinature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }
    
}

ii. 配置 applicationContext.xml

<!-- 配置 bean -->
<bean id="counter"
    class="package.CounterImp"></bean>
    
<!-- 配置切面的 bean -->
<bean id="loggingAspect"
    class="package.LoggingAspect"></bean>
    
<!-- 配置 AOP -->
<aop:config>
    <!-- 配置切点表达式 -->
    <aop:pointcut expression="execution(* package.*(..))"
        id="pointcut" />
    <!-- 配置切面及通知,order 属性为切面的优先级,数字越小,优先级越高 -->
    <aop:aspect ref="loggingAspect" order="2">
        <aop:before method="beforeMethod" pointcut-ref="pointcut" />
        <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result" />
    </aop:aspect>
</aop:config>

iii. 创建一个测试类:Main.java

public class Main{
    public static void main(String[] args){
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Counter counter = (Counter) ctx.getBean("counter");
        
        System.out.pritln(counter.getClass().getName());
        
        int result = counter.add(1, 2);
        System.out.println("-->" + result);
        
        result = counter.sub(2, 1);
        System.out.println("-->" + result);
        
    }
}

iiii.运行结果

$Proxy3

The method add begins with [1, 2]
The method add ends with 3
-->3
The method sub begins with [2, 1]
The method sub ends with 1
-->1

相关文章

网友评论

      本文标题:Spring(1) - AOP 编程

      本文链接:https://www.haomeiwen.com/subject/zchwvttx.html