动态代理,没有被代理对象静态代理和动态代理
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对象
//维护一个目标对象
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直接生成代理对象,没有被代理对象,因为连被代理实现类都没有
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);
}
}
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;
整个没有被代理对象