• 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:企业应用开发的思考和策略_DynaProxy


    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
     * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class GunDog implements Dog
    {
        //info方法实现,仅仅打印一个字符串
        public void info()
        {
            System.out.println("我是一只猎狗");
        }
        //run方法实现,仅仅打印一个字符串
        public void run()
        {
            System.out.println("我奔跑迅速");
        }
    }
    import java.lang.reflect.*;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class MyInvokationHandler
        implements InvocationHandler
    {
        // 需要被代理的对象
        private Object target;
        public void setTarget(Object target)
        {
            this.target = target;
        }
        // 执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法
        public Object invoke(Object proxy, Method method, Object[] args)
            throws Exception
        {
            TxUtil tx = new TxUtil();
            // 执行TxUtil对象中的beginTx()。
            tx.beginTx();
            // 以target作为主调来执行method方法
            Object result = method.invoke(target , args);
            // 执行TxUtil对象中的endTx()。
            tx.endTx();
            return result;
        }
    }
    import java.lang.reflect.*;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class MyProxyFactory
    {
        // 为指定target生成动态代理对象
        public static Object getProxy(Object target)
            throws Exception
        {
            // 创建一个MyInvokationHandler对象
            MyInvokationHandler handler = new MyInvokationHandler();
            // 为MyInvokationHandler设置target对象
            handler.setTarget(target);
            // 创建、并返回一个动态代理
            return Proxy.newProxyInstance(target.getClass().getClassLoader()
                , target.getClass().getInterfaces(), handler);
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class Test
    {
        public static void main(String[] args)
            throws Exception
        {
            // 创建一个原始的GunDog对象,作为target
            Dog target = new GunDog();
            // 以指定的target来创建动态代理
            Dog dog = (Dog)MyProxyFactory.getProxy(target);
            // 调用代理对象的info()和run()方法
            dog.info();
            dog.run();
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class TxUtil
    {
        // 第一个拦截器方法:模拟事务开始
        public void beginTx()
        {
            System.out.println("=====模拟开始事务=====");
        }
        // 第二个拦截器方法:模拟事务结束
        public void endTx()
        {
            System.out.println("=====模拟结束事务=====");
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public interface Dog
    {
        // info()方法声明
        void info();
        // run()方法声明
        void run();
    }
  • 相关阅读:
    Kinect学习笔记(六)——深度数据测量技术及应用
    [device]/proc/devices and /dev/
    [Eth]Mac/Phy/mdio/Rgmii
    [uboot]uboot如何引导系统
    [网络]Linux一些网络知识
    [基础]sizeof和strlen
    [基础]关于extern指针和数组的用法
    [ucos]了解ucos
    [Linux]gcc/libc/glibc
    [i.MX6q]i.MX6q处理器,linux操作系统平台搭建 从SD卡启动系统
  • 原文地址:https://www.cnblogs.com/tszr/p/12373108.html
Copyright © 2020-2023  润新知