zoukankan      html  css  js  c++  java
  • AOP 面向切面编程详解

    引言

    对类(class)增强的手段通常分为以下三类

    1.继承  子类通过继承父类或者实现接口对类进行增强   

    2.装饰者模式(Wrapper) 常用于对类的某个方法进行重写,以实现更强大的功能.常用于场合缓冲流,Struct2中StructRequestWarpper类   实现准则:是你还有你,一切全靠你

    3.动态代理 一句话概括:获取实现了一组接口的类对象 需提供目标对象,增强建议  三大参数 :ClassLoader Class[] interfaces InvocationHandler  (调用目标对象的方法时会调用这)

    区别与装饰者模式:装饰者模式通常是对一个类进行加强,而动态代理是获取一组接口的方法在对类进行增强

    具体实现

    package com.atguigu.spring.aop;
    
    public interface ArithmeticCalculator {
    
    	int add(int i, int j);
    	int sub(int i, int j);
    	
    	int mul(int i, int j);
    	int div(int i, int j);
    	
    }
    

      

    package com.atguigu.spring.aop;
    
    import org.springframework.stereotype.Component;
    
    @Component("arithmeticCalculator")
    public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    
    	@Override
    	public int add(int i, int j) {
    		int result = i + j;
    		return result;
    	}
    
    	@Override
    	public int sub(int i, int j) {
    		int result = i - j;
    		return result;
    	}
    
    	@Override
    	public int mul(int i, int j) {
    		int result = i * j;
    		return result;
    	}
    
    	@Override
    	public int div(int i, int j) {
    		int result = i / j;
    		return result;
    	}
    
    }
    

      要对Arithmetic 的add,sub,mul,div方法进行增强,添加日志方法,用到了代理

    传统的做法

    package com.atguigu.spring.aop;
    
    public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {
    
    	@Override
    	public int add(int i, int j) {
    		System.out.println("The method add begins with [" + i + "," + j + "]");
    		int result = i + j;
    		System.out.println("The method add ends with " + result);
    		return result;
    	}
    
    	@Override
    	public int sub(int i, int j) {
    		System.out.println("The method sub begins with [" + i + "," + j + "]");
    		int result = i - j;
    		System.out.println("The method sub ends with " + result);
    		return result;
    	}
    
    	@Override
    	public int mul(int i, int j) {
    		System.out.println("The method mul begins with [" + i + "," + j + "]");
    		int result = i * j;
    		System.out.println("The method mul ends with " + result);
    		return result;
    	}
    
    	@Override
    	public int div(int i, int j) {
    		System.out.println("The method div begins with [" + i + "," + j + "]");
    		int result = i / j;
    		System.out.println("The method div ends with " + result);
    		return result;
    	}
    
    }
    

      由以上代码可以看出,相似重复的代码太多,要对改类进行重写成更简单的类,用到了到动态代理

    package com.atguigu.spring.aop;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.util.Arrays;
    
    public class ArithmeticCalculatorLoggingProxy {
    	
    	//要代理的对象
    	private ArithmeticCalculator target;
    	
    	public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
    		super();
    		this.target = target;
    	}
    
    	//返回代理对象
    	public ArithmeticCalculator getLoggingProxy(){
    		ArithmeticCalculator proxy = null;
    		
    		ClassLoader loader = target.getClass().getClassLoader();
    		Class [] interfaces = new Class[]{ArithmeticCalculator.class};
    		InvocationHandler h = new InvocationHandler() {
    			/**
    			 * proxy: 代理对象。 一般不使用该对象
    			 * method: 正在被调用的方法
    			 * args: 调用方法传入的参数
    			 */
    			@Override
    			public Object invoke(Object proxy, Method method, Object[] args)
    					throws Throwable {
    				String methodName = method.getName();
    				//打印日志
    				System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));
    				
    				//调用目标方法
    				Object result = null;
    				
    				try {
    					//前置通知
    					result = method.invoke(target, args);
    					//返回通知, 可以访问到方法的返回值
    				} catch (NullPointerException e) {
    					e.printStackTrace();
    					//异常通知, 可以访问到方法出现的异常
    				}
    				
    				//后置通知. 因为方法可以能会出异常, 所以访问不到方法的返回值
    				
    				//打印日志
    				System.out.println("[after] The method ends with " + result);
    				
    				return result;
    			}
    		};
    		
    		/**
    		 * loader: 代理对象使用的类加载器。 
    		 * interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法. 
    		 * h: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法
    		 */
    		proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
    		
    		return proxy;
    	}
    }
    

      

  • 相关阅读:
    error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
    E: 无法获得锁 /var/lib/apt/lists/lock
    卸载nginx
    服务器文件传输
    一些重要的sql命令
    设置某个字段的值是唯一的
    mysql注释的方法
    认证 (authentication) 和授权 (authorization) 的区别
    neutron_lib: 一个进程内的发布与订阅
    人工智能导论作业
  • 原文地址:https://www.cnblogs.com/lt123/p/7348444.html
Copyright © 2011-2022 走看看