AOP动态代理

1.JDK动态代理

UserDao类:

public interface UserDao {
    public void addUser();
}

UserDaoImpl类:

public class UserDaoImpl implements UserDao {
    @Override
    public void addUser() {
        System.out.println("添加用户");
    }
}

MyAspect切面:

public class MyAspect {
    public void check_permissions(){
        System.out.println("检查权限");
    }
    public void log(){
        System.out.println("记录日志");
    }
}

JdkProxy代理类:

public class JdkProxy implements InvocationHandler {
    private UserDao userDao;

    public Object createProxy(UserDao userDao) {
        this.userDao = userDao;
        //1.类加载器
        ClassLoader loader = JdkProxy.class.getClassLoader();
        //2.被代理对象实现的所有接口
        Class[] classes = userDao.getClass().getInterfaces();
        //3.使用代理类进行增强,返回代理后的对象
        return Proxy.newProxyInstance(loader, classes, this);

    }

    /**
     * @param proxy  被代理后的对象
     * @param method 将要被执行方法的信息
     * @param args   执行方法时需要的参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //创建切面
        MyAspect myAspect = new MyAspect();
        //前增强
        myAspect.check_permissions();
        //调用方法,并传参
        Object obj = method.invoke(userDao, args);
        //后增强
        myAspect.log();
        return obj;
    }
}

Test测试类:

public class Test {
    public static void main(String[] args) {
        //创建代理对象
        JdkProxy jdkProxy = new JdkProxy();
        //创建目标对象
        UserDao userDao = new UserDaoImpl();
        //从代理对象中获取增强后的对象
        UserDao userDao1 = (UserDao) jdkProxy.createProxy(userDao);
        userDao1.addUser();
    }
}

 运行结果:

 

 

2.CGLIB代理

UserDao类:

public class UserDao {
    public void delete(){
        System.out.println("删除用户");
    }
}

MyAspect切面类同上

CglibProxy代理类:

import com.chauncey.jdk.MyAspect;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {
    public Object createProxy(Object target){
        //创建动态对象
        Enhancer enhancer = new Enhancer();
        //确定要增强的类
        enhancer.setSuperclass(target.getClass());
        //添加回调函数
        enhancer.setCallback(this);
        //返回代理类
        return enhancer.create();
    }

    /**
     *
     * @param o CGLIB根据指定父类生成对象
     * @param method 拦截的方法
     * @param objects 拦截方法参数的数组
     * @param methodProxy 方法的代理对象,用于执行父类方法
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        MyAspect myAspect = new MyAspect();
        myAspect.check_permissions();
        Object obj = methodProxy.invokeSuper(o,objects);
        myAspect.log();
        return null;
    }
}

Test测试类:

public class Test {
    public static void main(String[] args) {
        //创建代理对象
        CglibProxy proxy = new CglibProxy();
        //创建目标类
        UserDao userDao = new UserDao();
        //获取增强后的目标对象
        UserDao userDao1 = (UserDao) proxy.createProxy(userDao);
        userDao1.delete();
    }
}

运行结果:

 

posted @ 2019-03-11 22:28  笺笙  阅读(206)  评论(0编辑  收藏  举报