当前位置:首页 > CN2资讯 > 正文内容

动态代理,没有被代理对象静态代理和动态代理

3天前CN2资讯



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;


 

整个没有被代理对象

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/23989.html

    分享给朋友:

    “动态代理,没有被代理对象静态代理和动态代理” 的相关文章

    云计算技术在犬类健康管理中的应用与创新

    云计算服务在犬类健康管理中的应用 在现代社会中,科技的发展为我们的生活带来了许多便利,尤其是云计算技术提供了不可或缺的支持。在犬类健康管理中,云计算的应用同样发挥着至关重要的作用。这一技术不仅能帮助宠物主人更好地管理爱犬的健康状况,还可以提高宠物医院的服务效率和医疗水平。 首先,云计算技术的核心在于...

    如何选择合适的IP站及其运作原理

    IP站的运作原理 在探讨IP站的运作原理之前,首先需要明确什么是IP站。简单来说,IP站是一种特殊的网络服务,它利用互联网协议(IP)提供不同的网络功能和服务。每个IP站都与一个或多个IP地址相连,能够用来访问信息、数据或应用程序。在我的网络体验中,无论是个人使用还是企业应用,IP站总是扮演着至关重...

    GoDaddy虚拟主机全面指南:轻松搭建您理想的网站

    GoDaddy虚拟主机概述 提到GoDaddy,很多人首先会想到它作为域名注册商的地位。但是,GoDaddy不仅限于此,提供虚拟主机服务的实力也不容小觑。对于想要在网上建立个人博客、企业站点或者电子商务平台的朋友来说,GoDaddy的虚拟主机服务是一个值得考虑的选项。它的多样性和可靠性,吸引了不少新...

    AWS在日本的云计算市场发展与投资前景分析

    在讨论AWS在日本的市场背景时,我觉得日本的云计算市场是一个非常吸引人的话题。日本的经济科技发展水平相对较高,企业和政府机构对于云计算的接受度和需求不断增加。这种需求尤为体现于各个行业,比如金融、医疗、教育等。许多传统的行业正在努力向数字化转型,寻找更高效、可靠的解决方案。 2011年,AWS决定在...

    奈飞检测脚本:保障账号安全的有效解决方案

    在了解奈飞检测脚本之前,先从它的定义说起。简单来说,奈飞检测脚本是一种用于自动化检测奈飞账户状态的程序。通过这类脚本,用户能够快速确认自己的奈飞账号是否存在被盗、被共享或其他潜在的安全问题。这样一来,用户可以及时采取措施,保护自己的账户安全。 接下来,聊聊奈飞检测脚本的工作原理。这些脚本通常通过模拟...

    AWS新加坡评测:用户体验与性价比全面分析

    在探索AWS在新加坡的使用体验时,服务的可用性是我最先关注的方面。AWS 在全球范围内的广泛服务网络给予用户许多选择。在新加坡,AWS 提供了多种计算、存储和数据库等服务,让我感觉使用起来非常方便。尤其是在需要快速部署解决方案或应对业务高峰时,AWS 新加坡的数据中心承载能力和稳定性非常令人满意。无...