zoukankan      html  css  js  c++  java
  • 16_3.jdk动态代理与aop

    jdk动态代理:

    public interface Subject {
        void say(String name,int age);
    }
    
    public class RealSubject implements Subject {
        @Override
        public void say(String name, int age) {
            System.out.println("say...");
        }
    }
    
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    public class MyInvocation implements InvocationHandler {
        public static void main(String[] args) throws InstantiationException, IllegalAccessException {
            RealSubject instance = new RealSubject();
            MyInvocation myInvocation = new MyInvocation(instance);
    
            Subject o = (Subject) Proxy.newProxyInstance(instance.getClass().getClassLoader(), instance.getClass().getInterfaces(), myInvocation);
            o.say("p",12);
    
        }
        private Subject target;
        public MyInvocation(Subject subject) throws IllegalAccessException, InstantiationException {
            target = subject;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object invoke = method.invoke(target, args);
            return invoke;
        }
    }
    

    aop

    
    public interface Dog {
        void info();
        void run();
    }
    
    
    public class HuntingDog implements Dog {
        @Override
        public void info() {
            System.out.println("我是一只猎狗");
        }
        @Override
        public void run() {
            System.out.println("我奔跑迅速");
        }
    }
    
    
    public class DogUtil {
        public void method1(){
            System.out.println("=====模拟通用方法一=====");
        }
        public void method2(){
            System.out.println("=====模拟通用方法二=====");
        }
    }
    
    
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class MyInvocationHandler implements InvocationHandler {
        private Object target;
        public void setTarget(Object target){
            this.target = target;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            DogUtil dogUtil = new DogUtil();
            dogUtil.method1();
            Object res = method.invoke(target, args);
            dogUtil.method2();
            return res;
        }
    }
    
    
    import java.lang.reflect.Proxy;
    
    public class MyProxyFactory {
        public static Object getProxy(Object target){
            MyInvocationHandler handler = new MyInvocationHandler();
            handler.setTarget(target);
            return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),handler);
        }
    }
    
  • 相关阅读:
    实现用户注册验证码
    自带的打印预览
    分页存储过程
    文章标题、内容、摘要的处理函数
    ASP常用函数收藏
    生活中的经典感人语句
    如何在某一数据库的所有表的所有列上搜索一个字符串?
    如何访问隐藏的列表 workflow history list
    Windows Server 2008下如果什么操作没能正常完成, 请尝试run as administrator
    Visual Studio Build Marcos
  • 原文地址:https://www.cnblogs.com/fly-book/p/11549817.html
Copyright © 2011-2022 走看看