您好,登錄后才能下訂單哦!
本篇文章為大家展示了sharding-jdbc中WrapperAdapter的作用是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
jdk-12.jdk/Contents/Home/lib/src.zip!/java.sql/java/sql/Wrapper.java
public interface Wrapper { <T> T unwrap(java.lang.Class<T> 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 Collection<JdbcMethodInvocation> jdbcMethodInvocations = new ArrayList<>(); @SuppressWarnings("unchecked") @Override public final <T> T unwrap(final Class<T> 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的作用是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。