国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Spring代理模式

2019-11-15 01:11:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
SPRing代理模式

Spring代理模式大致分為靜態(tài)代理模式和動(dòng)態(tài)代理模式。

二者區(qū)別:

靜態(tài)代理模式:靈活性不強(qiáng);如果有1000個(gè)dao;就有1000個(gè)代理類(lèi);沒(méi)有做到通用性

靜態(tài)代理模式:

package StaticProxy;public interface PersonDao {        public void sava();        public void update();}
package StaticProxy;public class PersonDaoimpl implements PersonDao {    public String user = null;    PersonDaoimpl(){}    /**     * @return the user     */    public String getUser() {        return user;    }    /**     * @param user the user to set     */    public void setUser(String user) {        this.user = user;    }        PersonDaoimpl(String user){        this.user = user;    }        @Override    public void sava() {        // TODO Auto-generated method stub        System.out.println("進(jìn)入方法");    }    @Override    public void update() {        // TODO Auto-generated method stub            }}
package StaticProxy;import JDKProxy.impl.PersonDaoImpl;//靜態(tài)代理類(lèi)public class personproxy implements PersonDao{    public PersonDaoimpl persondaoimpl;        public personproxy(PersonDaoimpl persondaoimpl){        this.persondaoimpl = persondaoimpl;    }        @Override    public void sava() {        // TODO Auto-generated method stub        if(persondaoimpl.getUser() != null){            persondaoimpl.sava();        }    }    @Override    public void update() {        // TODO Auto-generated method stub            }    }

測(cè)試類(lèi):

package StaticProxy;public class Test {        public static void main(String[] args) {        //創(chuàng)建代理類(lèi)        personproxy pro  = new personproxy(new PersonDaoimpl());        pro.sava();    }}

效果圖:

動(dòng)態(tài)代理模式:JDK : proxy要求:目標(biāo)類(lèi)要有實(shí)現(xiàn)接口

package JDKProxy;//接口public interface PersonDao {        public void sava(String name);    public void update(String name);    public String getUsername(String name);}
package JDKProxy.impl;import JDKProxy.PersonDao;public class PersonDaoImpl implements PersonDao {    private String user = null;        /**     * @return the user     */    public String getUser() {        return user;    }    /**     * @param user the user to set     */    public void setUser(String user) {        this.user = user;    }    public PersonDaoImpl(){}        public PersonDaoImpl(String user){        this.user = user;    }        @Override    public String getUsername(String name) {            System.out.println("這是getusername()方法");                return "oooo";    }    @Override    public void sava(String name) {                System.out.println("這是save()方法");            }    @Override    public void update(String name) {                System.out.println("這是update()方法");            }}
package JDKProxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import JDKProxy.impl.PersonDaoImpl;/** * 代理工廠 *  * @author Administrator *  */public class JdkProxyFactory implements InvocationHandler {    private Object targetObject;    /**     * 生成代理對(duì)象     *      * @param targetObject     * @return     */    public Object createProxyObject(Object targetObject) {        this.targetObject = targetObject;        // 生成代理對(duì)象        // loader:目標(biāo)類(lèi)的加載器        // interfaces:目標(biāo)類(lèi)的所有接口        // 回調(diào)接口類(lèi)        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),                targetObject.getClass().getInterfaces(), this);    }    /**     * 代理類(lèi)回調(diào)方法 在調(diào)用目標(biāo)類(lèi)方法的時(shí)候進(jìn)行回調(diào)     * 調(diào)用目標(biāo)類(lèi)方法時(shí)進(jìn)行攔截     */    @Override//環(huán)繞通知    public Object invoke(Object proxy, Method method, Object[] args)            throws Throwable {        PersonDaoImpl persondaoimpl = (PersonDaoImpl) this.targetObject;        Object invoke = null;                try {            //....begin():前置通知            if (persondaoimpl.getUser() != null) {                // 委派目標(biāo)對(duì)象調(diào)用方法:調(diào)用的目標(biāo)方法稱(chēng)為連接點(diǎn)                invoke = method.invoke(targetObject, args);            }            //.....commit():后置通知                    } catch (Exception e) {            //callback():異常通知        }finally{            //close():最終通知        }                return invoke;    }}

測(cè)試:

package JDKProxy;import JDKProxy.impl.PersonDaoImpl;public class JdkProxyTest {    public static void main(String[] args) {        //創(chuàng)建動(dòng)態(tài)代理工廠        JdkProxyFactory jdkproxy = new JdkProxyFactory();        //為目標(biāo)類(lèi)創(chuàng)建代理對(duì)象        PersonDao persondao = (PersonDao) jdkproxy                .createProxyObject(new PersonDaoImpl(""));        persondao.update("xxx");    }}

效果圖;

您可以通過(guò)點(diǎn)擊 右下角 的按鈕 來(lái)對(duì)文章內(nèi)容作出評(píng)價(jià), 也可以通過(guò)左下方的 關(guān)注按鈕 來(lái)關(guān)注我的博客的最新動(dòng)態(tài)。 如果文章內(nèi)容對(duì)您有幫助, 不要忘記點(diǎn)擊右下角的 推薦按鈕 來(lái)支持一下哦   如果您對(duì)文章內(nèi)容有任何疑問(wèn), 可以通過(guò)評(píng)論或發(fā)郵件的方式聯(lián)系我: 2276292708@QQ.com如果需要轉(zhuǎn)載,請(qǐng)注明出處,謝謝!!

  


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汉阴县| 台北市| 荣昌县| 泰和县| 淄博市| 瓦房店市| 泸州市| 新乡市| 钟祥市| 六安市| 建平县| 中西区| 临澧县| 栾城县| 蒙自县| 寿阳县| 新宁县| 颍上县| 中牟县| 吴川市| 四子王旗| 黄梅县| 基隆市| 始兴县| 嘉黎县| 朔州市| 综艺| 六枝特区| 滨海县| 万源市| 和平县| 安阳县| 霍山县| 犍为县| 兴仁县| 西城区| 崇州市| 和林格尔县| 巩留县| 灵寿县| 贵南县|