cglib学习

转载的,不过链接找不到了。
这个例子就类似于jdk动态代理,但是由cglib实现,网上很多人都说cglib在性能上有优势,我也就记录在这里当学习了,这个例子的目的就是有点aop的感念,不破坏源代码,但是可以在执行原方法之前和之后做点事情。
感觉例子只要够简单,针对性强,还是看代码测试下比较好,自己动手实验要比看别人的解释来的更有效果吧。

InfoManager.java
package org.chuck.cglib;

/**
 * 信息管理
 * 
 * @author Maurice Jin
 */
public class InfoManager {

    // 模拟查询操作
    public void query() {
        System.out.println("query...");
    }

    // 模拟创建操作
    public void create() {
        System.out.println("create...");
    }

    // 模拟更新操作

    public void update() {
        System.out.println("update...");
    }

    // 模拟删除操作
    public void delete() {
        System.out.println("delete...");
    }

}



InfoManagerFactory.java
package org.chuck.cglib;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

/**
 * 工厂类
 * 
 * @author Maurice Jin
 */
public class InfoManagerFactory {

    private static InfoManager manger = new InfoManager();

    /**
     * 创建原始的InfoManager
     * 
     * @return
     */
    public static InfoManager getInstance() {
        return manger;
    }

    /**
     * 创建带有权限检验的InfoManager
     * 
     * @param auth
     * @return
     */
    public static InfoManager getAuthInstance(AuthProxy auth) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(InfoManager.class);
        enhancer.setCallback(auth);
        return (InfoManager) enhancer.create();
    }

    /**
     * 创建不同权限要求的InfoManager
     * 
     * @param auth
     * @return
     */
    public static InfoManager getSelectivityAuthInstance(AuthProxy auth) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(InfoManager.class);
        enhancer.setCallbacks(new Callback[] { auth, NoOp.INSTANCE });
        enhancer.setCallbackFilter(new AuthProxyFilter());
        return (InfoManager) enhancer.create();
    }

}



AuthProxy.java
package org.chuck.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 权限校验
 * 
 * @author Maurice Jin
 */
public class AuthProxy implements MethodInterceptor {

    private String name; // 会员登录名

    public AuthProxy(String name) {
        this.name = name;
    }

    /**
     * 权限校验,如果会员名为:maurice,则有权限做操作,否则提示没有权限
     */
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        if (!"maurice".equals(this.name)) {
            System.out.println("AuthProxy:you have no permits to do manager!");
            return null;
        }
        return proxy.invokeSuper(obj, args);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}


Client.java
package org.chuck.cglib;

/**
 * 客户端调用
 * 
 * @author Maurice Jin
 */
public class Client {

    public static void main(String[] args) {
        Client c = new Client();
        
//        c.anyonecanManager();
        
//        c.haveNoAuthManager();
//        c.haveAuthManager();
        
        c.selectivityAuthManager();
    }

    /**
     * 模拟:没有任何权限要求,任何人都可以操作
     */
    public void anyonecanManager() {
        System.out.println("any one can do manager...");
        InfoManager manager = InfoManagerFactory.getInstance();
        doCRUD(manager);
        separatorLine();
    }

    /**
     * 模拟:登录会员没有权限
     */
    public void haveNoAuthManager() {
        System.out.println("the loginer's name is not maurice,so have no permits do manager...");
        InfoManager noAuthManager = InfoManagerFactory.getAuthInstance(new AuthProxy("maurice1"));
        doCRUD(noAuthManager);
        separatorLine();
    }

    /**
     * 模拟:登录会员有权限
     */
    public void haveAuthManager() {
        System.out.println("the loginer's name is maurice,so have permits do manager...");
        InfoManager authManager = InfoManagerFactory.getAuthInstance(new AuthProxy("maurice"));
        doCRUD(authManager);
        separatorLine();
    }

    /**
     * 模拟:没有权限的会员,可以作查询操作
     */
    public void selectivityAuthManager() {
        System.out.println("the loginer's name is not maurice,so have no permits do manager except do query operator...");
        InfoManager authManager = InfoManagerFactory.getSelectivityAuthInstance(new AuthProxy("maurice1"));
        doCRUD(authManager);
        separatorLine();
    }

    /**
     * 对Info做增加/更新/删除/查询操作
     * 
     * @param manager
     */
    private void doCRUD(InfoManager manager) {
        manager.create();
        manager.update();
        manager.delete();
        manager.query();
    }

    /**
     * 加一个分隔行,用于区分
     */
    private void separatorLine() {
        System.out.println("################################");
    }

}



AuthProxyFilter.java
package org.chuck.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.CallbackFilter;

/**
 * proxy过滤
 * 
 * @author Maurice Jin
 */
public class AuthProxyFilter implements CallbackFilter {

    private static final int AUTH_NEED     = 0;
    private static final int AUTH_NOT_NEED = 1;

    /**
     * <pre>
     * 选择使用的proxy
     * 如果调用query函数,则使用第二个proxy
     * 否则,使用第一个proxy
     * </pre>
     */
    public int accept(Method method) {
        if ("query".equals(method.getName())) {
            return AUTH_NOT_NEED;
        }
        return AUTH_NEED;
    }

}

你可能感兴趣的:(AOP,C++,c,.net,C#)