Sub topic 18
Creating proxies by hand with ProxyFactory
This chapter assumes you know Spring AOP proxies, @Aspect and pointcuts from the earlier chapters. It covers three things: building a proxy without a container, adding interfaces to existing beans, and knowing when proxies are no longer enough.
Creating proxies by hand with ProxyFactory
ProxyFactory is the class Spring uses internally to build proxies. You can use it directly in a unit test or in a plain Java program. It picks a JDK proxy when the target implements an interface and CGLIB otherwise.
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
interface GreetingService {
String greet(String name);
}
class GreetingServiceImpl implements GreetingService {
public String greet(String name) {
return "Hello, " + name;
}
}
public class ProxyFactoryDemo {
public static void main(String[] args) {
ProxyFactory factory = new ProxyFactory(new GreetingServiceImpl());
factory.addAdvice((MethodInterceptor) invocation -> {
System.out.println("calling " + invocation.getMethod().getName());
return invocation.proceed();
});
GreetingService proxy = (GreetingService) factory.getProxy();
System.out.println(proxy.greet("Grace"));
System.out.println(AopUtils.isJdkDynamicProxy(proxy));
}
}The program prints calling greet, then Hello, Grace, then true. Call factory.setProxyTargetClass(true) to force CGLIB.
Introductions: adding interfaces with @DeclareParents
An introduction makes a bean implement an interface its class never declared. The proxy gets the extra interface, and calls to its methods go to a separate mixin object.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;
interface Auditable {
void markSeen();
boolean isSeen();
}
class DefaultAuditable implements Auditable {
private boolean seen;
public void markSeen() { seen = true; }
public boolean isSeen() { return seen; }
}
@Aspect
@Component
class AuditableIntroduction {
@DeclareParents(value = "com.example.service.*+",
defaultImpl = DefaultAuditable.class)
public static Auditable mixin;
}The + in the type pattern matches the listed types and their subtypes. Any matching bean can then be cast:
((Auditable) orderService).markSeen();Spring creates one mixin instance per proxy, so state such as seen is per bean.
When to move to AspectJ weaving
Spring AOP only intercepts public calls that arrive through a proxy on Spring beans. Switch to AspectJ weaving when you need any of these:
- Advice on self-invocation, private methods, or
finalclasses. - Advice on objects created with
new, or on field access and constructors. - Lower call overhead on very hot paths.
AspectJ changes the bytecode itself, so no proxy is involved.
| Weaving mode | When it happens | Cost |
|---|---|---|
| Compile-time | The AspectJ compiler or Maven plugin builds the aspects into your classes | Extra build step |
| Load-time | A Java agent weaves classes as they load | JVM agent and `aop.xml` setup |
Exercise
A bean's class has no interfaces and you add advice with ProxyFactory. Which proxy type do you get, and how do you check it?
Show answer
You get a CGLIB proxy, because there is no interface to build a JDK proxy from. Check it with AopUtils.isCglibProxy(proxy), which returns true. AopUtils.isJdkDynamicProxy(proxy) returns false.