欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AOP:jdk的动态代理

程序员文章站 2022-07-12 14:50:06
...

1.文件结构

AOP:jdk的动态代理

2.建立接口

package com.wangcf.manager;


public interface IUserManager {
    
    public void add();
    
    public void del();
    
    public void update();
    
    public String load(int uid);
}

3.实现接口,里面的所有方法都是切入点PointCut

package com.wangcf.manager;


public class UserManagerImp implements IUserManager{
    @Override
    public void add(){
        System.out.println("UserManagerImp add...");
    }
    @Override
    public void del(){
        System.out.println("UserManagerImp del...");
    }
    @Override
    public void update(){
        System.out.println("UserManagerImp update...");
    }

    @Override
    public String load(int uid) {
        // TODO Auto-generatuided method stub
        System.out.println("UserManagerImp load...");
        return "asda"+uid;
    }
}

4.切面拦截,切面类aspect

package com.wangcf.manager;

public class LogManager {
    //advice(通知)
    public static void addLog(){
        System.out.println("添加日志...");
    }
}

5.获取代理类

package com.wangcf.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.wangcf.manager.LogManager;

public class MyInvocationHandler implements InvocationHandler{

    private Object target;//目标对象
    
    public MyInvocationHandler(Object target) {
        // TODO Auto-generated constructor stub
        this.target=target;
    }
    
    public Object newProxy(){
        Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), 
                this);
        return proxy;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("MyInvocationHandler invoke...");
        /*
         * 
         */
        LogManager.addLog();
        //去执行目标对象的方法
        System.out.println("args: "+args);
        //去执行目标对象的方法
        Object returnValue = method.invoke(target, args);
        System.out.println("returnValue:"+returnValue);
        return returnValue;
    }

}

6.测试类

package com.wangcf.test;

import com.wangcf.handler.MyInvocationHandler;
import com.wangcf.manager.IUserManager;
import com.wangcf.manager.UserManagerImp;

public class TestAction {
    public static void main(String[] args) {
     //目标对象userManager
        IUserManager userManager=new UserManagerImp();
        
        //下面这段获取代理
        MyInvocationHandler mh=new MyInvocationHandler(userManager); 
        Object proxy=mh.newProxy();
        System.out.println(proxy.getClass().getName());
        IUserManager userManagerProxy=(IUserManager)proxy; 
        
        //不带参数add,和load方法称为连接点JoinPoint
        userManagerProxy.add();
        System.out.println("==============");
        //带参数
        userManagerProxy.load(10);
    }
}

7.输出:

AOP:jdk的动态代理

8.总结:

Proxy0是动态获取的代理

通过连接点进入切面,LogManager为切面类