面向切面之AOP原理浅析

[b][color=red]先上代码[/color][/b]


package com.dzeay.aop;

public interface IBusiness {
public void excute();
}

package com.dzeay.aop;

public class BusinessImpl implements IBusiness {

public void excute() {
System.out.println("BusinessImpl.excute");
}
}

package com.dzeay.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class LogHandler implements InvocationHandler {

private Object delegate;

public LogHandler(Object delegate) {
this.delegate = delegate;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object obj = null;
try {
System.out.println(method + "\tbegin");
obj = method.invoke(delegate, args);
System.out.println(method + "\tend");
} catch (Exception e) {
}

return obj;
}
}

package com.dzeay.aop;

import java.lang.reflect.Proxy;

public class TestClass {

/**
* @param args
*/
public static void main(String[] args) {
IBusiness businessImp = new BusinessImpl();

IBusiness proxy = (IBusiness) Proxy.newProxyInstance(businessImp.getClass().getClassLoader(), businessImp.getClass().getInterfaces(), new LogHandler(businessImp));

proxy.excute();
}
}
    原文作者:AOP
    原文地址: https://blog.csdn.net/iteye_10145/article/details/82008863
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞