标签:nts pack port 生成 实例 cat handle 客户 int
为啥要用代理??
代理可以对原有逻辑进行增强,比如我们去租房,可以去找中介(也就是代理),而不是我们自己找。
代理主要就是对原有方法进行增强的。
代理分为静态代理和动态代理。
package designpattern.proxy.staticproxy;
/**
* 接口:租房
*/
public interface IRentingHouse {
void rentHosue();
}
package designpattern.proxy.staticproxy;
import designpattern.proxy.staticproxy.IRentingHouse;
public class RentingHouseImpl implements IRentingHouse {
@Override
public void rentHosue() {
System.out.println("我要租用一室一厅的房子");
}
}
package designpattern.proxy.staticproxy;
import designpattern.proxy.staticproxy.IRentingHouse;
public class RentingHouseProxy implements IRentingHouse {
private IRentingHouse rentingHouse;
public RentingHouseProxy(IRentingHouse rentingHouse) {
this.rentingHouse = rentingHouse;
}
@Override
public void rentHosue() {
System.out.println("中介(代理)收取服务费3000元");
rentingHouse.rentHosue();
System.out.println("客户信息卖了3毛钱");
}
}
package designpattern.proxy.staticproxy;
import designpattern.proxy.staticproxy.IRentingHouse;
import designpattern.proxy.staticproxy.RentingHouseImpl;
import designpattern.proxy.staticproxy.RentingHouseProxy;
public class Test {
public static void main(String[] args) {
IRentingHouse rentingHouse = new RentingHouseImpl();
// 自己要租用一个一室一厅的房子
// rentingHouse.rentHosue();
RentingHouseProxy rentingHouseProxy = new RentingHouseProxy(rentingHouse);
rentingHouseProxy.rentHosue();
}
}
静态代理和动态代理的区别主要是动态代理看不到代理类,由底层通过反射实现
分为jdk动态代理和Cglib动态代理
jdk动态代理所代理的对象必须实现接口(因为要传入),而Cglib代理不需要。
package designpattern.proxy.dynamicproxy;
/**
* 接口:租房
* jdk动态代理/cglib动态代理
*/
public interface IRentingHouse {
void rentHosue();
}
package designpattern.proxy.dynamicproxy;
import designpattern.proxy.dynamicproxy.IRentingHouse;
/**
* 委托方(委托对象)
*/
public class RentingHouseImpl implements IRentingHouse {
@Override
public void rentHosue() {
System.out.println("我要租用一室一厅的房子");
}
}
package designpattern.proxy.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author 应癫
*/
public class JdkProxy {
public static void main(String[] args) {
IRentingHouse rentingHouse = new RentingHouseImpl(); // 委托对象---委托方
// 从代理对象工厂获取代理对象
IRentingHouse jdkProxy = (IRentingHouse) ProxyFactory.getInstance().getJdkProxy(rentingHouse);
jdkProxy.rentHosue();
}
}
package designpattern.proxy.dynamicproxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author 应癫
*
*
* 代理对象工厂:生成代理对象的
*/
public class ProxyFactory {
private ProxyFactory(){
}
private static ProxyFactory proxyFactory = new ProxyFactory();
public static ProxyFactory getInstance() {
return proxyFactory;
}
/**
* Jdk动态代理
* @param obj 委托对象
* @return 代理对象
*/
public Object getJdkProxy(Object obj) {
// 获取代理对象
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
// 写增强逻辑
System.out.println("中介(代理)收取服务费3000元");
// 调用原有业务逻辑
result = method.invoke(obj,args);
System.out.println("客户信息卖了3毛钱");
return result;
}
});
}
/**
* 使用cglib动态代理生成代理对象
* @param obj 委托对象
* @return
*/
public Object getCglibProxy(Object obj) {
return Enhancer.create(obj.getClass(), new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
Object result = null;
System.out.println("中介(代理)收取服务费3000元");
result = method.invoke(obj,objects);
System.out.println("客户信息卖了3毛钱");
return result;
}
});
}
}
package designpattern.proxy.dynamicproxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* @author 应癫
*/
public class CglibProxy {
public static void main(String[] args) {
RentingHouseImpl rentingHouse = new RentingHouseImpl(); // 委托对象
// 获取rentingHouse对象的代理对象,
// Enhancer类似于JDK动态代理中的Proxy
// 通过实现接口MethodInterceptor能够对各个方法进行拦截增强,类似于JDK动态代理中的InvocationHandler
// 使用工厂来获取代理对象
RentingHouseImpl cglibProxy = (RentingHouseImpl) ProxyFactory.getInstance().getCglibProxy(rentingHouse);
cglibProxy.rentHosue();
}
}
再来一个JDK动态代理的实例:
package com.example.jdkproxy;
public interface Person {??? public void dosomething();
}
package com.example.jdkproxy;
public class Bob implements Person{
@Override
public void dosomething() {
System.out.println("Bob do something.");
}
}
package com.example.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author liuyj
* @Title: JDKDynamicProxy
* @create 2020-06-02 17:30
* @ProjectName test
* @Description: JDK动态代理
*/
public class JDKDynamicProxy implements InvocationHandler {
//声明被代理的对象
private Person person;
//构造函数
public JDKDynamicProxy(Person person) {
this.person = person;
}
//获取代理对象
public Object getTarget(){
Object proxyInstance = Proxy.newProxyInstance(person.getClass().getClassLoader(), person.getClass().getInterfaces(), this);
return proxyInstance;
}
//代理对象执行方法的时候都会执行此方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("对原方法进行前置增强。");
//原方法执行
Object invoke = method.invoke(person, args);
System.out.println("对原方法进行后置增强。");
return invoke;
}
}
package com.example.jdkproxy;
public class ProxyTest {
public static void main(String[] args) {
System.out.println("不使用代理类。。。");
Person person=new Bob();
person.dosomething();
System.out.println("--------------");
System.out.println("使用代理类。。。");
Person proxy = (Person) new JDKDynamicProxy(new Bob()).getTarget();
proxy.dosomething();
}
}
标签:nts pack port 生成 实例 cat handle 客户 int
原文地址:https://www.cnblogs.com/liuyj-top/p/13092351.html