Master Java Proxy Patterns: From Static to Dynamic and MyBatis Integration

This article explains static and dynamic proxy patterns in Java, demonstrates how to use them for recording service response times, and shows how MyBatis leverages dynamic proxies to generate mapper implementations, complete with UML diagrams and sample code.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Java Proxy Patterns: From Static to Dynamic and MyBatis Integration

1. Static Proxy

During the autumn recruitment season, many developers face the need to record service response times; this scenario can be abstracted into the proxy pattern.

ISubject: abstract of the visited resource

SubjectImpl: concrete implementation of the visited resource (e.g., landlord)

SubjectProxy: proxy implementation of the visited resource (e.g., intermediary)

UML diagram:

Example: a boss asks to record the response time of a user service using a static proxy.

When the boss also wants to record product service response times, three more classes (IProductService, ProductServiceImpl, ProductServiceProxy) are needed, illustrating the duplication problem of static proxies.

2. Dynamic Proxy

Since JDK 1.3, Java introduced Dynamic Proxy, allowing runtime generation of proxy objects for specified interfaces, solving the duplication issue of static proxies.

Dynamic proxy relies on java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler.

By implementing a single InvocationHandler, we can create proxies for both IUserService and IProductService to add the same cross‑cutting logic.

UML diagram of the dynamic proxy implementation:

3. MyBatis Core Design

MyBatis uses dynamic proxy to generate implementations of mapper interfaces at runtime. Role role = roleMapper.getRole(3L); The generated class is org.apache.ibatis.binding.MapperProxy, which corresponds to the SubjectProxy in the earlier UML.

Thus, MyBatis’s core idea is to map interface methods to SQL statements via a proxy, eliminating the need for manual implementation classes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsJavaBackend DevelopmentMyBatisDynamic ProxyProxy Pattern
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

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.