Aop动态代理解决问题

使用动态代理解决上述问题

代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.

package com.learn.spring.aop;

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

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;
	}
	
}
package com.learn.spring.aop;

public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		//
		System.out.println("learn==>The method add begins with ["+i+","+j+"]");
		int result = i + j ;
		//
		System.out.println("learn==>The method add ends with "+ result );
		return result;
	}

	@Override
	public int sub(int i, int j) {
		System.out.println("learn==>The method sub begins with ["+i+","+j+"]");
		int result = i - j ;
		System.out.println("learn==>The method sub ends with "+ result );
		return result;	
	}

	@Override
	public int mul(int i, int j) {
		System.out.println("learn==>The method mul begins with ["+i+","+j+"]");
		int result = i * j ;
		System.out.println("learn==>The method mul ends with "+ result );
		return result;
	}

	@Override
	public int div(int i, int j) {
		System.out.println("learn==>The method div begins with ["+i+","+j+"]");
		int result = i / j ;
		System.out.println("learn==>The method div ends with "+ result );
		return result;
	}
	
}
package com.learn.spring.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

//代理类
public class ArithmeticCalculatorLoggingProxy {

	//1.被代理的对象  目标对象
	private ArithmeticCalculator target ; // 实际上是ArithmeticCalculatorImpl对象.
	
	//通过构造器的方式将目标对象传入
	public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target){
		this.target = target ;
	}
	
	//获取代理对象
	public ArithmeticCalculator getLoggingProxy(){
		//定义代理对象
		ArithmeticCalculator proxy ;
		
		/**
		 * loader:  ClassLoader 类加载器
		 * interfaces:  目标类的所有接口,目的是获取接口中的方法
		 * h: InvocationHandler
		 */
		ClassLoader loader = target.getClass().getClassLoader();
		Class[] interfaces = target.getClass().getInterfaces();
		InvocationHandler h = new InvocationHandler() {
			/**
			 * proxy:代理对象   在invoke方法中一般不会用
			 * method:正在调用的方法
			 * args:调用方法传入的参数
			 */
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				String methodName = method.getName();
				//加日志
				System.out.println("learn===>The method "+methodName+" begins with "+ Arrays.asList(args));
				//执行目标方法
				Object result = method.invoke(target, args);
				//加日志
				System.out.println("learn===>The method "+methodName+" ends with " + result );
				return result;
			}
		};
		
		proxy = (ArithmeticCalculator)Proxy.newProxyInstance(loader, interfaces, h);
		
		return proxy ;
	}
	
	
}
package com.learn.spring.aop;

public class Main {
	public static void main(String[] args) {
		//目标对象
		ArithmeticCalculator target = new ArithmeticCalculatorImpl();
		//获取代理对象
		ArithmeticCalculator  proxy = 
				new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
		System.out.println("proxy:" + proxy.getClass().getName());
		
		int result = proxy.add(1, 1);   // 实际上要去执行invoke方法
		System.out.println("result=====>"+result);
	}
}
发布了2545 篇原创文章 · 获赞 66 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/105569475