静态代理与动态代理(jdk,cglib)
程序员文章站
2022-03-20 22:57:46
举个例子,我们租房可以自己找房源,也可以找中介,中介就相当于代理。静态代理(1)租房接口IRentingHousepublic interface IRentingHouse { void renting();}(2)租房接口实现类RentingHouseImpl//自己找房源public class RentingHouseImpl implements IRentingHouse{ @Override public void renting() {...
举个例子,我们租房可以自己找房源,也可以找中介,中介就相当于代理。
静态代理
(1)租房接口IRentingHouse
public interface IRentingHouse {
void renting();
}
(2)租房接口实现类RentingHouseImpl
//自己找房源
public class RentingHouseImpl implements IRentingHouse{
@Override
public void renting() {
System.out.println("我要租房");
}
}
(3)中介代理类,他也需要找房子所以也需要实现IRentingHouse这个接口,代理类中需要声明当前接口的属性
public class RentingHouseProxy implements IRentingHouse {
private IRentingHouse rentingHouse;
public RentingHouseProxy(IRentingHouse rentingHouse) {
this.rentingHouse = rentingHouse;
}
@Override
public void renting() {
System.out.println("中介找房源");
rentingHouse.renting();
System.out.println("签订合同,交服务费");
}
}
(4)测试类Test
public class Test {
public static void main(String[] args) {
IRentingHouse rentingHouse = new RentingHouseImpl();
RentingHouseProxy rentingHouseProxy = new RentingHouseProxy(rentingHouse);
rentingHouseProxy.renting();
}
}
执行结果
中介找房源
我要租房
签订合同,交服务费
静态代理: 需要对每个业务都写一个代理类,比较冗余,这就是它与动态代理的区别。
动态代理
动态代理分为jdk动态代理和cglib动态代理,简单讲下区别:
JDK动态代理只能对实现了接口的类生成代理,而不能针对类;
CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法(继承);
下面分别用jdk动态代理和cglib动态代理来实现这个需求吧
jdk动态代理
IRentingHouse接口和实现类RentingHouseImpl还是老样子,贴一下代码
public interface IRentingHouse {
void renting();
}
//自己找房源
public class RentingHouseImpl implements IRentingHouse{
@Override
public void renting() {
System.out.println("我要租房");
}
}
代理工厂类,生成代理对象
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 代理工厂类-饿汉
*/
public class ProxyFactory {
private ProxyFactory() {
}
private static ProxyFactory proxyFactory = new ProxyFactory();
public static ProxyFactory getInstance() {
return proxyFactory;
}
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("中介找房源");
result = method.invoke(obj, args);
System.out.println("签订合同,交服务费");
return result;
}
});
}
}
测试类Test
public class Test {
public static void main(String[] args) {
IRentingHouse rentingHouse = new RentingHouseImpl();
IRentingHouse jdkProxy = (IRentingHouse) ProxyFactory.getInstance().getJdkProxy(rentingHouse);
jdkProxy.renting();
}
}
执行结果
中介找房源
我要租房
签订合同,交服务费
cglib动态代理
首先pom.xml中添加依赖
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_2</version>
</dependency>
ProxyFactory中新加方法
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("中介找房源");
result = method.invoke(obj, objects);
System.out.println("签订合同,交服务费");
return result;
}
});
}
测试类
public class Test {
public static void main(String[] args) {
RentingHouseImpl rentingHouse1 = new RentingHouseImpl();
RentingHouseImpl cglibProxy = (RentingHouseImpl) ProxyFactory.getInstance().getCglibProxy(rentingHouse1);
cglibProxy.renting();
}
}
本文地址:https://blog.csdn.net/weixin_32896095/article/details/110233562
上一篇: Angularjs中的ui-bootstrap的使用教程
下一篇: C语言实现扫雷游戏简易版