Backend Development 4 min read

Custom MyBatis Interceptor Mechanism – Java Dynamic Proxy

This article explains how to create a custom MyBatis Interceptor using Java dynamic proxies, detailing the Interceptor interface, the plugin generation process, the wrap and invoke methods, and common use‑cases such as pagination, SQL analysis, encryption, and performance monitoring.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Custom MyBatis Interceptor Mechanism – Java Dynamic Proxy

In MyBatis, extending functionality requires implementing the Interceptor interface, which defines three methods: intercept(Invocation) for custom business logic, plugin(Object) as the entry point for proxy generation, and setProperties(Properties) for optional configuration.

The Interceptor interface is defined as:

public interface Interceptor {
  Object intercept(Invocation invocation) throws Throwable;
  default Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
  default void setProperties(Properties properties) {
    // NOP
  }
}

The plugin() method delegates to Plugin.wrap , which creates a dynamic proxy that routes method calls to the interceptor when the method matches the configured signatures.

public static Object wrap(Object target, Interceptor interceptor) {
    Map
, Set
> signatureMap = getSignatureMap(interceptor);
    Class
type = target.getClass();
    Class
[] interfaces = getAllInterfaces(type, signatureMap);
    if (interfaces.length > 0) {
      return Proxy.newProxyInstance(
          type.getClassLoader(),
          interfaces,
          new Plugin(target, interceptor, signatureMap));
    }
    return target;
}

The generated proxy implements InvocationHandler and its invoke method checks whether the invoked method is among the intercepted signatures; if so, it calls interceptor.intercept , otherwise it proceeds with the original method.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  try {
    Set
methods = signatureMap.get(method.getDeclaringClass());
    if (methods != null && methods.contains(method)) {
      return interceptor.intercept(new Invocation(target, method, args));
    }
    return method.invoke(target, args);
  } catch (Exception e) {
    throw ExceptionUtil.unwrapThrowable(e);
  }
}

The dynamic proxy generation is triggered by org.apache.ibatis.plugin.InterceptorChain#pluginAll , which applies the interceptor to core MyBatis components such as ParameterHandler , ResultSetHandler , StatementHandler , and Executor .

Typical application scenarios for custom interceptors include implementing pagination plugins, analyzing SQL to prevent full‑table updates or deletions, encrypting/decrypting fields, measuring DAO execution time for slow‑SQL reporting, and dynamic data‑source switching, among others.

For more examples, you can explore the MyBatis‑Plus open‑source components, many of which are built using custom interceptors.

backendJavaSQLMyBatisInterceptorDynamic Proxy
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.