Sub topic 3
The proxy pattern: a stand-in that adds behavior
This chapter assumes you know Java interfaces, inheritance and basic reflection, and that you have used Spring beans with dependency injection. It covers the proxy pattern, which Spring AOP uses to apply cross-cutting behavior.
What a proxy is
A proxy is an object that has the same type as another object, called the target, and stands in for it. Callers hold the proxy. When they call a method, the proxy can run extra code and then pass the call to the target.
Because the proxy and the target share a type, the caller cannot tell them apart and needs no changes. The extra code lives in one place, outside the business class.
A hand-written proxy
You can write a proxy yourself by implementing the same interface and holding a reference to the target.
interface GreetingService {
String greet(String name);
}
class GreetingServiceImpl implements GreetingService {
public String greet(String name) {
return "Hello, " + name;
}
}
class LoggingGreetingProxy implements GreetingService {
private final GreetingService target;
LoggingGreetingProxy(GreetingService target) {
this.target = target;
}
public String greet(String name) {
System.out.println("before greet");
return target.greet(name);
}
}
public class HandWrittenProxyDemo {
public static void main(String[] args) {
GreetingService service = new LoggingGreetingProxy(new GreetingServiceImpl());
System.out.println(service.greet("Ada"));
}
}The output is before greet followed by Hello, Ada. The caller only sees a GreetingService.
The problem with writing proxies by hand
A hand-written proxy handles one interface. To add logging to ten services, you would write ten proxy classes with nearly identical code. Each new method on an interface also forces an update to every proxy of it.
The fix is to generate proxies at runtime. One piece of code, such as a timing routine, then applies to many types. The next chapters show how the JDK and CGLIB generate these proxies.
Proxy and target are different objects
Two facts matter later in this tutorial:
- The proxy and the target are two separate objects. Only calls that reach the proxy get the extra behavior.
- A call the target makes on itself does not go through the proxy.
Where Spring fits
Spring creates a proxy for a bean and puts the proxy in the container in place of the original object. Beans that inject it receive the proxy. You write the extra behavior as an aspect, and Spring builds the proxy for you.
| Role | Example in this chapter |
|---|---|
| Target | `GreetingServiceImpl` |
| Proxy | `LoggingGreetingProxy` |
| Extra behavior | Printing `before greet` |
| Caller | `HandWrittenProxyDemo.main` |
Exercise
A proxy for OrderService wraps a target. Inside the target, placeOrder() calls this.sendEmail(). You added logging behavior to the proxy for every method. Is sendEmail() logged when a caller invokes placeOrder() on the proxy? Explain why.
Show answer
Only placeOrder() is logged. The caller invoked placeOrder() on the proxy, so its extra behavior runs. Inside the target, this refers to the target and not the proxy, so the call to sendEmail() bypasses the proxy and is not logged.
Exercise
Why is a hand-written proxy class a poor fit for applying one behavior, such as logging, to many different services?
Show answer
Each proxy class implements one specific interface, so you need a separate class for every service and must update it whenever the interface changes. Generating proxies at runtime lets one piece of code serve many types.