静态代理
被代理类
package ink.yql.proxy;
public class Test01
{
public void eat(){
System.out.println("我在吃饭");
}
public void run(){
System.out.println("我在跑步");
}
public void sport(){
System.out.println("我在运动");
}
}
静态代理类
package ink.yql.proxy;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StaticProxy extends Test01{
private Test01 test;
public StaticProxy(Test01 test){
this.test = test;
}
@Override
public void run() {
System.out.println("run方法开始前的时间 : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
test.run();
System.out.println("run方法结束的时间 : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void eat() {
System.out.println("eat方法开始前的时间 : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
test.eat();
System.out.println("eat方法结束的时间 : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void sport() {
System.out.println("sport方法开始前的时间 : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
test.sport();
System.out.println("sport方法结束的时间 : " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试
package ink.yql.proxy;
import java.lang.reflect.Proxy;
public class Test02 {
public static void main(String[] args) {
Test01 proxy = new StaticProxy(new Test01());
proxy.run();
proxy.eat();
proxy.sport();
}
}
动态代理
JDK动态代理
被代理类
package ink.yql.proxy;
public class Test01
implements Test
{
public void eat(){
System.out.println("我在吃饭");
}
public void run(){
System.out.println("我在跑步");
}
public void sport(){
System.out.println("我在运动");
}
}
代理类
package ink.yql.proxy;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKDynamicProxy implements InvocationHandler {
private Object obj;
Object newInstance(){
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces()
, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("当前方法名 : " +method.getName());
Object invoke = method.invoke(obj, args);
System.out.println("当前 : " +method.getName() +"方法 执行完毕");
return invoke;
}
public JDKDynamicProxy(Object o){
obj = o;
}
}
测试
package ink.yql.proxy;
import java.lang.reflect.Proxy;
public class Test02 {
public static void main(String[] args) {
Test01 test01 = new Test01();
JDKDynamicProxy jdkDynamicProxy = new JDKDynamicProxy(test01);
// Object o = jdkDynamicProxy.newInstance();
Object o = Proxy.newProxyInstance(test01.getClass().getClassLoader(), test01.getClass().getInterfaces(),
jdkDynamicProxy);
((Test)o).run();
}
}
Cglib动态代理
被代理类
package ink.yql.proxy;
public class Test01
{
public void eat(){
System.out.println("我在吃饭");
}
public void run(){
System.out.println("我在跑步");
}
public void sport(){
System.out.println("我在运动");
}
}
代理类
package ink.yql.proxy;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CglibDynamicProxy implements MethodInterceptor {
//有一个被代理对象
private Object obj;
public Object newInstance(){
Enhancer cm = new Enhancer();
cm.setSuperclass(obj.getClass());
cm.setCallback(this);
return cm.create();
}
public CglibDynamicProxy(Object obj) {
this.obj = obj;
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("当前方法名 : " +method.getName());
Object invoke = method.invoke(obj, objects);
System.out.println("当前 : " +method.getName() +"方法 执行完毕");
return invoke;
}
}
测试
package ink.yql.proxy;
import java.lang.reflect.Proxy;
public class Test02 {
public static void main(String[] args) {
Test01 test01 = new Test01();
test01 = (Test01)new CglibDynamicProxy(test01).newInstance();
test01.run();
test01.sport();
}
}
public class Test02 {
public static void main(String[] args) {
Test01 test01 = new Test01();
test01 = (Test01)new CglibDynamicProxy(test01).newInstance();
test01.run();
test01.sport();
}
}