1 概述
- 把核心功能和辅助功能分开
- 将核心功能与辅助功能(事务、日志、性能监控代码)分离,达到核心业务功能更纯粹、辅助业务功能可复用。
2 静态代理
- 通过代理类的对象,为原始类的对象(目标类的对象)添加辅助功能,更容易更换代理实现类、利于维护。
- 代理和被代理类之间实现相同的接口,代理调用被代理类中的方法,在此基础上添加赋值的功能
ChuZu接口
package com.sw.staticProxy;
public interface ChuZu {
Double zuFang(Double money);
}
FangDong
package com.sw.staticProxy;
public class FangDong implements ChuZu {
@Override
public Double zuFang(Double money) {
System.out.println("房东收取租金..." + money);
return money*0.95;
}
}
ZhongJie
package com.sw.staticProxy;
public class ZhongJie implements ChuZu {
@Override
public Double zuFang(Double money) {
FangDong fangDong = new FangDong();
System.out.println("中介发布租房广告...");
System.out.println("中介带看房子...");
System.out.println("中介组织租客和房东签订合同...");
fangDong.zuFang(money*0.9);
System.out.println("中介提供服务...");
return money*0.9;
}
}
测试
package com.sw;
import com.sw.staticProxy.FangDong;
import com.sw.staticProxy.ZhongJie;
import org.junit.Test;
public class TestStaticChuZu {
@Test
public void chuZu01(){
FangDong fangDong = new FangDong();
Double ret = fangDong.zuFang(2000.0);
}
@Test
public void chuZu02(){
ZhongJie zhongJie = new ZhongJie();
Double ret = zhongJie.zuFang(2000.0);
}
}
3 JDK动态代理
- 基于接口
- 代理对象转换成被代理对象实现的接口的类型
- 在处理程序方法中调用被代理对象的核心业务方法,添加辅助功能
ChuZu
package com.sw.jdkProxy;
public interface ChuZu {
Double chuZu(Double money);
}
FangDong
package com.sw.jdkProxy;
public class FangDong implements ChuZu {
@Override
public Double chuZu(Double money) {
System.out.println("房东出租房屋");
return money;
}
}
动态代理
package com.sw.jdkProxy;
import org.junit.Test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class TestJdk {
@Test
public void getZu(){
FangDong fangDong = new FangDong();
ChuZu proxyInstance = (ChuZu) Proxy.newProxyInstance(
fangDong.getClass().getClassLoader(),
fangDong.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("中介发布广告...");
System.out.println("中介带看房子...");
System.out.println("中介组织签合同...");
Double ret = (Double) args[0] * 0.9;
Object object = method.invoke(fangDong, ret);
System.out.println("中介提供服务...");
return object;
}
}
);
Double ret = proxyInstance.chuZu(2000.0);
System.out.println(ret);
}
}
4 CGlib动态代理
FangDong
package com.sw.jdkProxy;
public class FangDong {
public Double chuZu(Double money) {
System.out.println("房东出租房屋");
return money;
}
}
代理对象
package com.sw.cglibProxy;
import com.sw.jdkProxy.FangDong;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;
import java.lang.reflect.Method;
public class TestCGlib {
@Test
public void getCG(){
FangDong fangDong = new FangDong();
FangDong proxyInstance = (FangDong) Enhancer.create(
FangDong.class,
new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("中介发布广告..");
System.out.println("中介带看房子..");
System.out.println("中介组织签合同..");
Double ret = (Double) args[0] * 0.9;
Object object = method.invoke(fangDong, ret);
System.out.println("中介提供服务..");
return object;
}
}
);
Double ret = proxyInstance.chuZu(2000.0);
System.out.println(ret);
}
}
5 自定义创建代理对象的方法
public <T> T getMapper(Class<T> clazz){
T proxyInstance = (T) Enhancer.create(
clazz,
new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("中介发布广告..");
System.out.println("中介带看房子..");
System.out.println("中介组织签合同..");
Double ret = (Double) args[0] * 0.9;
Object object = method.invoke(clazz.newInstance(), ret);
System.out.println("中介提供服务..");
return object;
}
}
);
return proxyInstance;
}
@Test
public void getProxy(){
FangDong proxy = getMapper(FangDong.class);
Double ret = proxy.chuZu(3000.0);
System.out.println(ret);
}