Spring 學習 之 動態代理實現AOP切面

接口

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);

	}

}

 

發佈了21 篇原創文章 · 獲贊 0 · 訪問量 3430
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章