一.代理模式概念
代理(Proxy)是一種設計模式, 提供了對目標對象另外的訪問方式;即通過代理訪問目標對象。 這樣好處: 可以在目標對象實現的基礎上,增強額外的功能操作。(擴展目標對象的功能)。
二.靜態(tài)代理
代理對象,要實現與目標對象一樣的接口
1.可以做到在不修改目標對象的功能前提下,對目標對象功能擴展。
2.缺點:
- a.因為代理對象,需要與目標對象實現一樣的接口。所以會有很多代理類,類太多。
- b.一旦接口增加方法,目標對象與代理對象都要維護。
接口類:StudentDao .java
package com.huan.a_static;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public interface StudentDao {
void delect();
}
目標對象:StudentDaoImpl.java
package com.huan.a_static;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class StudentDaoImpl implements StudentDao {
public void delect() {
System.out.println("刪除數據成功");
}
}
代理對象:StudentDaoProxy.java
package com.huan.a_static;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class StudentDaoProxy implements StudentDao {
//接收保存目標對象
private StudentDao target;
public StudentDaoProxy(StudentDao target){
this.target=target;
}
public void delect() {
System.out.println("開始事務");
target.delect();//執(zhí)行目標對象的方法
System.out.println("結束事務");
}
}
測試用例:
package com.huan.a_static;
import com.huan.BeanTest.UserDao;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class Test {
public static void main(String[] args){
//目標對象
StudentDao studentDao = new StudentDaoImpl();
//代理
StudentDao proxy = new StudentDaoProxy(studentDao);
proxy.delect();
}
}
三.動態(tài)代理
1)代理對象,不需要實現接口;
2)代理對象的生成,是利用JDKAPI, 動態(tài)的在內存中構建代理對象(需要我們指定創(chuàng)建 代理對象/目標對象 實現的接口的類型);
- 動態(tài)代理, JDK代理, 接口代理;
動態(tài)代理總結:
` 代理對象不需要實現接口,但是目標對象一定要實現接口;否則不能用動態(tài)代理!
接口類:StudentDao .java
package com.huan.b_dynameic;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public interface StudentDao {
void delect();
}
目標對象:StudentDaoImpl.java
package com.huan.b_dynameic;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class StudentDaoImpl implements StudentDao {
public void delect() {
System.out.println("刪除數據成功");
}
}
代理對象:StudentDaoProxy.java
package com.huan.b_dynameic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class ProxyFactory {
//接收保存目標對象
private Object target;
public ProxyFactory(Object target){
this.target = target;
}
//給目標對象生成代理對象
public Object getProxyFactory() {
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("開啟事務");
//執(zhí)行目標對象
Object returnValue = method.invoke(target,args);
System.out.println("提交事務");
return returnValue;
}
});
}
}
測試用例:
package com.huan.b_dynameic;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class Test {
public static void main(String[] args){
//目標對象
StudentDao studentDao =new StudentDaoImpl();
//代理
StudentDao proxy = (StudentDao) new ProxyFactory(studentDao).getProxyFactory();
//執(zhí)行方法
proxy.delect();
}
}
四.Cglib代理
Cglib代理,也叫做子類代理。在內存中構建一個子類對象從而實現對目標對象功能的擴展。
Cglib子類代理:
- 需要引入cglib – jar文件, 但是spring的核心包中已經包括了cglib功能,所以直接引入spring-core-3.2.5.jar即可。
2)引入功能包后,就可以在內存中動態(tài)構建子類
3)代理的類不能為final, 否則報錯。
4) 目標對象的方法如果為final/static, 那么就不會被攔截,即不會執(zhí)行目標對象額外的業(yè)務方法。
目標對象:StudentDaoImpl.java
package com.huan.c_cglib;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class StudentDaoImpl {
public void delect() {
System.out.println("刪除數據成功");
}
}
Cglib子類對象 ProxyFactory .java
package com.huan.c_cglib;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class ProxyFactory implements MethodInterceptor {
/**
* Cglib子類對象
* 對UserDao 在內存中構建一個子類對象
*/
//接收保存目標對象
private Object target;
public ProxyFactory(Object target){
this.target = target;
}
//給目標對象創(chuàng)建代理對象
public Object getProxyInstance(){
//工具類
Enhancer en = new Enhancer();
//設置父類
en.setSuperclass(target.getClass());
//設置回調函數
en.setCallback(this);
// 創(chuàng)建子類:代理對象對象
return en.create();
}
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("開始事務---");
method.invoke(target,objects);
System.out.println("結束事務----");
return null;
}
}
測試用例:
package com.huan.c_cglib;
/**
* Created by 馬歡歡 on 2017/6/25.
*/
public class Test {
public static void main(String[] args){
StudentDaoImpl studentDao = new StudentDaoImpl();
StudentDaoImpl proxy = (StudentDaoImpl) new ProxyFactory(studentDao).getProxyInstance();
proxy.delect();
}
}
上一篇:Spring--IOC容器——對象依賴關系(注解)