码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式&CGLIB代理

时间:2018-08-20 21:53:14      阅读:451      评论:0      收藏:0      [点我收藏+]

标签:不同的   war   eth   方法   打印   类的方法   ade   cts   inf   

面向大海,春暖花开

技术分享图片

 来自网友的解析:

Enhancer可能是CGLIB中最常用的一个类,和Java1.3动态代理中引入的Proxy类差不多(如果对Proxy不懂,可以参考这里)。和Proxy不同的是,Enhancer既能够代理普通的class,也能够代理接口。Enhancer创建一个被代理对象的子类并且拦截所有的方法调用(包括从Object中继承的toString和hashCode方法)。Enhancer不能够拦截final方法,例如Object.getClass()方法,这是由于Java final方法语义决定的。基于同样的道理,Enhancer也不能对fianl类进行代理操作。这也是Hibernate为什么不能持久化final class的原因

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.log4j.Logger;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 代理打印日志,并生检查登录,使用的原则是,name必须要放到第一个参数
 * @author Binglong
 * @date 2018-03-20
 */
public class RemoteServiceProxy{
    private static final Logger logger = Logger.getLogger(RemoteServiceProxy.class);

    private static class SingleInstance{
        private static final RemoteService INSTANCE = new RemoteServiceProxy().getProxy();
    }

    /**
     * 获取代理对象的单例
     * @return
     */
    public static RemoteService getProxyInstance(){
        return SingleInstance.INSTANCE;
    }

    /**
     * 获取代理对象
     * @return
     */
    public RemoteService getProxy(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(RemoteService.class);
        enhancer.setCallback(getMethodInterceptor());
        return (RemoteService)enhancer.create();
    }

    /**
     * SmcName必须要放到第一个参数
     * @return
     */
    public MethodInterceptor getMethodInterceptor(){
        return new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                String smcName = (String) objects[0];
                boolean flag = checkLogin(method.getName(), smcName);
                if (flag){
                    //执行父类的方法,也就是RemoteService中的方法
                    return methodProxy.invokeSuper(o, objects);
                }
                return null;
            }
        };
    }

    /**
     * 检查登录状态
     * @param methodName
     * @param name
     * @return
     */
    private boolean checkLogin(String methodName, String name){
        String logMsg = ‘[‘ + name + "][RemoteServiceProxy." + methodName + ‘]‘;
        if (logger.isDebugEnabled()) {
            logger.debug(logMsg + " begin...");
        }
        boolean loginFlag = RemoteService.getInstance().isLogined(name);
        if (!loginFlag) {
            logger.info(logMsg + " not login, login first");
            Map<String, String> header = SmcService.getInstance().header;
            loginFlag = RemoteService.getInstance().login(header, name);
            if (!loginFlag){
                logger.warn(logMsg + " login failed");
                return false;
            }
        }
        return true;
    }
}

 

单例模式&CGLIB代理

标签:不同的   war   eth   方法   打印   类的方法   ade   cts   inf   

原文地址:https://www.cnblogs.com/nedhome/p/9508021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!