1.blind方法即给被代理类对象实例话,又返回代理类对象
/**
* 代理类
*/
class MyInvocationHandler implements InvocationHandler{
Object object;//实现了接口的被代理类对象的声明(RealSubject)
// 1.给被代理类的对象实例话
//2.返回一个代理类的对象
public Object blind(Object obj){
this.object = obj;
//三个参数被代理类对象的classLoader,被代理类实现的接口,实现了InvocationHandler接口的类对象
return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
}
//当通过代理类的对象发起对被代理类方法的调用时,都会转为对invoke方法的调用,进而实现动态效果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//returnVal是被代理类方法的返回值
Object returnVal = method.invoke(object,args);
System.out.println("invoke方法被调用");
return returnVal;
}
}
调用:
public class TestDynamicAgent {
public static void main(String[] args) {
//1.构造被代理类的对象
RealSubject realSubject = new RealSubject();
//2创建一个实现了InvocationHandle接口的对象
MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
//3.通过myInvocationHandle的blind方法 ,返回实现了被代理类所实现的接口的代理类对象
Object obj = myInvocationHandler.blind(realSubject);
Subject sub = (Subject) obj;//sub就是i代理类的对象
sub.action();//转到对代理类的inovke方法的调用
// //测试动态代理
// NikeClothFactory clothFactory = new NikeClothFactory();
// Object demo = myInvocationHandler.blind(clothFactory);
// ClothFactory clo = (ClothFactory) demo;
// clo.productCloth();
}
}
2.构建一个静态方法返回代理类对象
//代理类
class MyInvocationHandler implements InvocationHandler{
Object object;
public void setObject(Object object) {
this.object = object;
}
//在methoo1与method2之间插入被代理类方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
HumanUtil util = new HumanUtil();
util.method1();
Object returnVal = method.invoke(object,args);
util.method2();
return returnVal;
}
}
class myProxy{
//动态创建代理类对象
public static Object getProxyInstance(Object obj){
//创建代理类对象
MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
myInvocationHandler.setObject(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),myInvocationHandler);
}
}
调用:
public class TestAop {
public static void main(String[] args) {
SuperMan superMan = new SuperMan();//被代理类对象
Object obj = myProxy.getProxyInstance(superMan);//代理类对象
Human human = (Human) obj;
human.info();
System.out.println();
human.fly();
}
}
3.匿名内部类:
package com.spring.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created by 小美女 on 2018/12/3.
*/
public class ArithmeticCalulatorLogProxy {
private ArithmeticCalulator target;
public ArithmeticCalulatorLogProxy(ArithmeticCalulator target){
this.target = target;
}
public ArithmeticCalulator getProxy(){
ArithmeticCalulator proxy = null;
InvocationHandler invocationHandler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("invoke");
Object returnVal = method.invoke(target,args);
return returnVal;
}
};
proxy = (ArithmeticCalulator) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),invocationHandler);
return proxy;
}
}
调用:
package com.spring.aop;
/**
* @Author: wj
* @Date: 2018/12/3 10:09
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ArithmeticCalulator arithmeticCalulator = new ArithmeticCalulatorImpl();
ArithmeticCalulatorLogProxy arithmeticCalulatorLogProxy = new ArithmeticCalulatorLogProxy(arithmeticCalulator);
ArithmeticCalulator a = arithmeticCalulatorLogProxy.getProxy();
int result = a.add(1,2);
System.out.println(result);
}
}