zoukankan      html  css  js  c++  java
  • 动态代理,没有被代理对象

    1 在mybatis Guice 事务源码解析中,可以发现SqlSessionManager中的sqlSessionProxy使用SqlSession构建的代理

        private SqlSessionManager(SqlSessionFactory sqlSessionFactory) {
            this.sqlSessionFactory = sqlSessionFactory;
            this.sqlSessionProxy = (SqlSession)Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionManager.SqlSessionInterceptor());
        }
    
    this.sqlSessionProxy = (SqlSession)Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionManager.SqlSessionInterceptor());

    ******************************
    private class SqlSessionInterceptor implements InvocationHandler {
    public SqlSessionInterceptor() {
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    SqlSession sqlSession = (SqlSession)SqlSessionManager.this.localSqlSession.get();
    if(sqlSession != null) {
    try {
    return method.invoke(sqlSession, args);
    } catch (Throwable var19) {
    throw ExceptionUtil.unwrapThrowable(var19);
    }
    } else {
    SqlSession autoSqlSession = SqlSessionManager.this.openSession();
    Throwable var6 = null;

    Object var8;
    try {
    try {
    Object t = method.invoke(autoSqlSession, args);
    autoSqlSession.commit();
    var8 = t;
    } catch (Throwable var20) {
    autoSqlSession.rollback();
    throw ExceptionUtil.unwrapThrowable(var20);
    }
    } catch (Throwable var21) {
    var6 = var21;
    throw var21;
    } finally {
    if(autoSqlSession != null) {
    if(var6 != null) {
    try {
    autoSqlSession.close();
    } catch (Throwable var18) {
    var6.addSuppressed(var18);
    }
    } else {
    autoSqlSession.close();
    }
    }

    }

    return var8;
    }
    }
    }

    可以看到没有维护被代理的target对象,用的时候即时生成被代理的DefaultSqlSession对象

    public class ProxyFactory implements InvocationHandler {
        //维护一个目标对象
        private Object target;
        public ProxyFactory(Object target){
            this.target = target;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("开始事务jdk");
            Object returnValue = method.invoke(target, args);
            System.out.println("提交事务jdk");
            return returnValue;
        }
    
        //给目标对象生成代理对象
        public Object getProxyInstance(){
            return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        }
    
    }
    
    public class ProxyFactoryNonTarget implements InvocationHandler {
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("开始事务jdk");
            Object returnValue = method.invoke(new UserDao(), args);
            System.out.println("提交事务jdk");
            return returnValue;
        }
    
        //给目标对象生成代理对象
        public Object getProxyInstance(){
            return Proxy.newProxyInstance(ProxyFactoryNonTarget.class.getClassLoader(), new Class[]{IUserDao.class}, this);
        }
    
    }
    

    下面的每次invoke都生成新对象

    2 mybatis本身就是根据interface直接生成代理对象,没有被代理对象,因为连被代理实现类都没有

    public class MapperProxyFactory<T> {
        private final Class<T> mapperInterface;
        private final Map<Method, MapperMethodInvoker> methodCache = new ConcurrentHashMap();
    
        public MapperProxyFactory(Class<T> mapperInterface) {
            this.mapperInterface = mapperInterface;
        }
    
        public Class<T> getMapperInterface() {
            return this.mapperInterface;
        }
    
        public Map<Method, MapperMethodInvoker> getMethodCache() {
            return this.methodCache;
        }
    
        protected T newInstance(MapperProxy<T> mapperProxy) {
            return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
        }
    
        public T newInstance(SqlSession sqlSession) {
            MapperProxy mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
            return this.newInstance(mapperProxy);
        }
    }
    
    public class MapperProxy<T> implements InvocationHandler, Serializable {
        private static final long serialVersionUID = -4724728412955527868L;
        private static final int ALLOWED_MODES = 15;
        private static final Constructor<Lookup> lookupConstructor;
        private static final Method privateLookupInMethod;
        private final SqlSession sqlSession;
    

    整个没有被代理对象

  • 相关阅读:
    JAVA基础知识|HTTP协议-两个特性
    JAVA基础知识|TCP/IP协议
    Spring Cloud|高可用的Eureka集群服务
    Hadoop环境搭建|第四篇:hive环境搭建
    C#中Func与Action的理解
    C# lambda表达式
    WPF ControlTemplate
    sublime text3插件安装及使用
    Dev Express之ImageComboBoxEdit,RepositoryItemImageComboBox使用方式
    SQL查询结果增加序列号
  • 原文地址:https://www.cnblogs.com/silyvin/p/13582293.html
Copyright © 2011-2022 走看看