对于消费者而言,可以减少成本,只需要关心自己需要的商品,不需要去寻找渠道或者房源
- 统一异常处理
- MyBatis
- Spring AOP
- 日志框架
代理模式(proxy pattern): 是23种设计模式中的一种,属于结构型的模式。
意义:目标对象只需要关系自己的实现细节,通过代理对象来实现功能的增强,可以扩展目标对象的功能
体现了非常重要的编程思想,不能随便修改源码,通过代理的方式来拓展
详细见连接中的静态代理:https://blog.csdn.net/hc1285653662/article/details/127199791
静态代理存在的问题
- 不利于代码的扩展,比如接口中新添加一个抽象方法,所有的实现类都需要做修改
- 代理对象需要创建许多
在不改变原有功能代码的前提下,能够动态的实现方法的增强
详细见连接中的动态代理:https://blog.csdn.net/hc1285653662/article/details/127199791
/**
* 生成代理类的字节码文件
* @param path
*/
public static void saveProxyClass(String path) {
byte[] $Proxy1s = ProxyGenerator.generateProxyClass("$Proxy1",
Calculator.class.getInterfaces());
FileOutputStream out = null;
try {
out = new FileOutputStream(path + "$Proxy1.class");
out.write($Proxy1s);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public final class $Proxy1 extends Proxy implements Calculate {
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy1(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int add(int var1, int var2) throws {
try {
return (Integer)super.h.invoke(this, m3, new Object[]{var1, var2});
} catch (RuntimeException | Error var4) {
throw var4;
} catch (Throwable var5) {
throw new UndeclaredThrowableException(var5);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("com.houchen.staticproxy.Calculate").getMethod("add", Integer.TYPE, Integer.TYPE);
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>2.2.2version>
dependency>
public class MyInvocationHandler implements InvocationHandler {
private Object target = null;
//动态代理,目标对象是活动的,不是固定的,需要传入进来
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("代理对象中进行功能增强 start....");
Object res = method.invoke(target, args);
System.out.println("代理对象中进行功能增强 end ....");
return res;
}
}
public class Test {
public static void main(String[] args) {
// 得到方法拦截器
CglibInterceptor cglibInterceptor = new CglibInterceptor();
// 使用CGLIB框架生成目标类的子类(代理类)实现增强
Enhancer enhancer = new Enhancer();
// 设置父类字节码文件
enhancer.setSuperclass(Calculator.class);
// 设置拦截处理
enhancer.setCallback(cglibInterceptor);
// 创建代理对象
Calculator proxy = (Calculator) enhancer.create();
int add = proxy.add(1, 2);
System.out.println(add);
}
}
public class Test {
public static void main(String[] args) {
// 生成cglib的目标代理类对象
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,"E:\\Code\\JavaSE\\ProxyModeTest\\src");
// 得到方法拦截器
CglibInterceptor cglibInterceptor = new CglibInterceptor();
// 使用CGLIB框架生成目标类的子类(代理类)实现增强
Enhancer enhancer = new Enhancer();
// 设置父类字节码文件
enhancer.setSuperclass(Calculator.class);
// 设置拦截处理
enhancer.setCallback(cglibInterceptor);
// 创建代理对象
Calculator proxy = (Calculator) enhancer.create();
int add = proxy.add(1, 2);
System.out.println(add);
}
}