[java]代码库
interface human{
void info();
void fly();
}
class SuperMan implements human{
public void info(){
System.out.println("我是超人");
}
public void fly(){
System.out.println("I can fly");
}
}
class HumanUtil{
public void method1(){
System.out.println("=========方法一=============");
}
public void method2(){
System.out.println("=========方法二=============");
}
}
class MyInvocationHandler implements InvocationHandler{
//被代理对象声明
Object obj;
public void setObject(Object obj){
this.obj=obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
HumanUtil h = new HumanUtil();
h.method1();
Object returlVal=method.invoke(obj, args);
h.method2();
return returlVal;
}
}
class MyProxy{
//动态的创建一个代理类对象
public static Object getProxyInstance(Object obj){
MyInvocationHandler handler = new MyInvocationHandler();
handler.setObject(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
}
}
public class testAop {
public static void main(String[] args){
SuperMan man= new SuperMan();
Object obj=MyProxy.getProxyInstance(man);
human hu=(human)obj;
hu.info();
System.out.println();
hu.fly();
}
}