zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级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();
    }
  • 相关阅读:
    Windbg 基本调试常识(转)
    善用VS中的Code Snippet来提高开发效率
    如何跟踪调试Software product?
    Visual Studio 2008 每日提示(二十七)
    6步确保 windbg 成功调试 .net(转)
    Visual Studio 2005 重置设置
    Print to Output /To trace runtime
    Windbg安装和配置(转)
    C++与C#交互
    All hands on deck
  • 原文地址:https://www.cnblogs.com/tszr/p/12373108.html
Copyright © 2011-2022 走看看