动静代理的性能:通过拦截器办法回调,对指标target办法进行加强。
话中有话就是为了加强指标target办法。下面这句话没错,但也不要认为它就是真谛,殊不知,动静代理还有投鞭断流的霸权,连指标target都不要的科幻模式。
注:本文默认认为,读者对动静代理的原理是了解的,如果不明确target的含意,难以看懂本篇文章,倡议先了解动静代理。
1.自定义JDK动静代理之投鞭断流实现主动映射器Mapper
首先定义一个pojo。
public class User {
private Integer id;
private String name;
private int age;
public User(Integer id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// getter setter
}
再定义一个接口UserMapper.java。
public interface UserMapper {
public User getUserById(Integer id);
}
接下来咱们看看如何应用动静代理之投鞭断流,实现实例化接口并调用接口办法返回数据的。
自定义一个InvocationHandler。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MapperProxy implements InvocationHandler {
@SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> clz) {
return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] { clz }, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
// 诸如hashCode()、toString()、equals()等办法,将target指向以后对象this
return method.invoke(this, args);
} catch (Throwable t) {
}
}
// 投鞭断流
return new User((Integer) args[0], "zhangsan", 18);
}
}
下面代码中的target,在执行Object.java内的办法时,target被指向了this,target曾经变成了傀儡、象征、占位符。在投鞭断流式的拦挡时,曾经没有了target。
写一个测试代码:
public static void main(String[] args) {
MapperProxy proxy = new MapperProxy();
UserMapper mapper = proxy.newInstance(UserMapper.class);
User user = mapper.getUserById(1001);
System.out.println("ID:" + user.getId());
System.out.println("Name:" + user.getName());
System.out.println("Age:" + user.getAge());
System.out.println(mapper.toString());
}
output:
ID:1001
Name:zhangsan
Age:18
x.y.MapperProxy@6bc7c054
这便是Mybatis主动映射器Mapper的底层实现原理。
可能有读者不禁要问:你怎么把代码写的像初学者写的一样?没有构造,且不足美感。
必须申明,作为一名教训老道的高手,能把程序写的像初学者写的一样,那必然是高手中的高手。这样能够让初学者感觉到亲切,难受,合乎本人的Style,让他们或她们,感觉到大牛写的代码也不过如此,本人甚至写的比这些大牛写的还要好,从此自信满满,热情高涨,认为与大牛之间的差距,仅剩下三分钟。
2.Mybatis主动映射器Mapper的源码剖析
首先编写一个测试类:
public static void main(String[] args) {
SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
try {
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.findAllStudents();
for (Student student : students) {
System.out.println(student);
}
} finally {
sqlSession.close();
}
}
Mapper长这个样子:
public interface StudentMapper {
List<Student> findAllStudents();
Student findStudentById(Integer id);
void insertStudent(Student student);
}
org.apache.ibatis.binding.MapperProxy.java局部源码。
public class MapperProxy<T> implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
private final SqlSession sqlSession;
private final Class<T> mapperInterface;
private final Map<Method, MapperMethod> methodCache;
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
// 投鞭断流
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
// ...
org.apache.ibatis.binding.MapperProxyFactory.java局部源码。
public class MapperProxyFactory<T> {
private final Class<T> mapperInterface;
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
这便是Mybatis应用动静代理之投鞭断流。
3.接口Mapper内的办法能重载(overLoad)吗?(重要)
相似上面:
public User getUserById(Integer id);
public User getUserById(Integer id, String name);
Answer:不能。
起因:在投鞭断流时,Mybatis应用package+Mapper+method全限名作为key,去xml内寻找惟一sql来执行的。相似:key=x.y.UserMapper.getUserById,那么,重载办法时将导致矛盾。对于Mapper接口,Mybatis禁止办法重载(overLoad)。
注:学习时,是先钻研的源码,看懂了原理。写博文时,则先阐释原理,再浏览的源码。程序刚好相同,心愿读者不要因而纳闷,认为我弱小到未卜先知。
作者:祖大俊
起源:my.oschina.net/zudajun/blog/666223
欢送关注我的微信公众号「码农解围」,分享Python、Java、大数据、机器学习、人工智能等技术,关注码农技术晋升•职场解围•思维跃迁,20万+码农成长充电第一站,陪有幻想的你一起成长
发表回复