第一种演示jdk动态代理分别演示一下
动态代理类要实现这个InvocationHandler接口
package com.example.ioc.dao;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDCProxy implements InvocationHandler {
Object target;
public JDCProxy(Object obj) {
target=obj;
}
public Object createProxy(){
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始时间"+System.currentTimeMillis());
Object obj=method.invoke(target,args);
System.out.println("结束时间"+System.currentTimeMillis());
return obj;
}
}
package com.example.ioc.service;
public interface Iperson {
public void show();
}
package com.example.ioc.service;
public class Person implements Iperson{
@Override
public void show() {
System.out.println("hello 这是jdk动态代理");
}
}
package com.example;
import com.example.autoconfig.UdaSpringContext;
import com.example.ioc.dao.JDCProxy;
import com.example.ioc.dao.UserDao;
import com.example.ioc.service.Iperson;
import com.example.ioc.service.Person;
import com.example.ioc.service.Userservice;
import java.lang.reflect.Proxy;
import java.util.Map;
public class TestApplication {
public static void main(String[] args){
Person person=new Person();
JDCProxy jdcProxy=new JDCProxy(person);
Iperson iperson= (Iperson) jdcProxy.createProxy();
iperson.show();
}
}
第二种是cglib的动态代理分别演示一下
动态代理类要实现这个MethodInterceptor接口,import org.springframework.cglib.proxy.MethodInterceptor;这个下面的
package com.example.ioc.dao;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.util.Date;
public class CglibProxy implements MethodInterceptor {
Object target;
public CglibProxy(Object obj) {
target=obj;
}
public Object createProxy(){
/*使用cglib动态代理
* 1.声明生成类实例,用于生产代理类
* */
Enhancer enhancer=new Enhancer();
//2.设置被代理类的字节码,cglib根据字节码生成被代理类的子类
enhancer.setSuperclass(target.getClass());
//设置回调函数,用一个方法拦截
enhancer.setCallback(this);
//创建代理
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("cglib动态代理开始"+new Date().toGMTString());
Object invoke = method.invoke(target, objects);
System.out.println("cglib动态代理结束"+new Date().toGMTString());
return invoke;
}
}
package com.example.ioc.service;
public interface Iperson {
public void show();
}
package com.example.ioc.service;
public class Person implements Iperson{
@Override
public void show() {
System.out.println("hello 这是Cglib动态代理");
}
}
package com.example;
import com.example.autoconfig.UdaSpringContext;
import com.example.ioc.dao.CglibProxy;
import com.example.ioc.dao.JDCProxy;
import com.example.ioc.dao.UserDao;
import com.example.ioc.service.Iperson;
import com.example.ioc.service.Person;
import com.example.ioc.service.Userservice;
import java.lang.reflect.Proxy;
import java.util.Map;
public class TestApplication {
public static void main(String[] args){
CglibProxy cglibProxy=new CglibProxy(new Person());
Iperson iperson = (Iperson) cglibProxy.createProxy();
iperson.show();
}
}