欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring 学习 之 动态代理实现AOP切面

程序员文章站 2022-03-03 12:49:24
...

接口

package com.spring.aop;

public interface ArtthemticCalculator {
	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.spring.aop;

public class ArtthemticCalculatorImpl implements ArtthemticCalculator {

	@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.spring.aop;

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

public class ArtthemticCalculatorLoggingProxy {

	// 要代理的对象
	private ArtthemticCalculator target;

	public ArtthemticCalculatorLoggingProxy(ArtthemticCalculator target) {
		
		this.target = target;
	}
	
	public ArtthemticCalculator getLoggingProxy(){
		
		ArtthemticCalculator proxy = null;
		
		// 代理对象由哪一个加载器负责加载
		ClassLoader loader = target.getClass().getClassLoader();
		
		// 代理对象的类型,即其中有哪些方法
		Class[] interfaces = new Class[]{ArtthemticCalculator.class};
		
		// 当调用代理对象方法时候,该执行的方法
		InvocationHandler h = new InvocationHandler(){

			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

				// 开始日志
				System.out.println("The method " + method.getName() + " begins with " + Arrays.asList(args));
				// 执行方法
				Object result = method.invoke(target, args);
				// 结束日志
				System.out.println("The method div ends with "+ result);
				return result;
			}
			
		};
		
		proxy = (ArtthemticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
		
		return proxy;
		
	}
	
}

测试类

package com.spring.aop;

public class Mainaop2 {

	public static void main(String[] args) {

//		ArtthemticCalculator a = new ArtthemticCalculatorImpl();
		
		ArtthemticCalculator target = new ArtthemticCalculatorImpl();
		ArtthemticCalculator proxy = new ArtthemticCalculatorLoggingProxy(target).getLoggingProxy();
		
//		int result = a.add(3, 8);
		int result = proxy.add(3, 8);
		System.out.println(result);
		
//		result = a.sub(16, 8);
		result = proxy.sub(16, 8);
		System.out.println(result);
		
//		result = a.mul(16, 8);
		result = proxy.mul(16, 8);
		System.out.println(result);
		
//		result = a.div(72, 8);
		result = proxy.div(72, 8);
		System.out.println(result);

	}

}

 

相关标签: spring 基础