本篇文章为大家展示了sharding-jdbc中WrapperAdapter的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
成都创新互联专注于衡阳企业网站建设,自适应网站建设,商城系统网站开发。衡阳网站建设公司,为衡阳等地区提供建站服务。全流程按需定制网站,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
jdk-12.jdk/Contents/Home/lib/src.zip!/java.sql/java/sql/Wrapper.java
public interface Wrapper {T unwrap(java.lang.Class iface) throws java.sql.SQLException; boolean isWrapperFor(java.lang.Class> iface) throws java.sql.SQLException; }
Wrapper接口定义了unwrap、isWrapperFor方法
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/WrapperAdapter.java
public abstract class WrapperAdapter implements Wrapper { private final CollectionjdbcMethodInvocations = new ArrayList<>(); @SuppressWarnings("unchecked") @Override public final T unwrap(final Class iface) throws SQLException { if (isWrapperFor(iface)) { return (T) this; } throw new SQLException(String.format("[%s] cannot be unwrapped as [%s]", getClass().getName(), iface.getName())); } @Override public final boolean isWrapperFor(final Class> iface) { return iface.isInstance(this); } /** * record method invocation. * * @param targetClass target class * @param methodName method name * @param argumentTypes argument types * @param arguments arguments */ @SneakyThrows public final void recordMethodInvocation(final Class> targetClass, final String methodName, final Class>[] argumentTypes, final Object[] arguments) { jdbcMethodInvocations.add(new JdbcMethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments)); } /** * Replay methods invocation. * * @param target target object */ public final void replayMethodsInvocation(final Object target) { for (JdbcMethodInvocation each : jdbcMethodInvocations) { each.invoke(target); } } }
WrapperAdapter声明实现java.sql.Wrapper接口,它定义了JdbcMethodInvocation集合;recordMethodInvocation方法会往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法则会挨个执行JdbcMethodInvocation的invoke方法
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/invocation/JdbcMethodInvocation.java
@RequiredArgsConstructor public class JdbcMethodInvocation { @Getter private final Method method; @Getter private final Object[] arguments; /** * Invoke JDBC method. * * @param target target object */ @SneakyThrows public void invoke(final Object target) { method.invoke(target, arguments); } }
JdbcMethodInvocation的invoke方法执行的是method.invoke
WrapperAdapter声明实现java.sql.Wrapper接口,它定义了JdbcMethodInvocation集合;recordMethodInvocation方法会往jdbcMethodInvocations添加JdbcMethodInvocation;replayMethodsInvocation方法则会挨个执行JdbcMethodInvocation的invoke方法
上述内容就是sharding-jdbc中WrapperAdapter的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。