spring-projects/spring-framework · error · IllegalArgumentException

Cannot create class proxy for TargetSource with null target

Error message

Cannot create class proxy for TargetSource with null target class

What it means

ProxyFactory.getProxy(TargetSource) creates a target-class-based CGLIB proxy. It explicitly rejects a TargetSource whose getTargetClass() returns null, because there is no class to subclass for the proxy. The check is a precondition guard before setProxyTargetClass(true).

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java:161

	 * @param proxyInterface the interface that the proxy should implement
	 * @param targetSource the TargetSource that the proxy should invoke
	 * @return the proxy object
	 * @see #ProxyFactory(Class, org.springframework.aop.TargetSource)
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getProxy(Class<T> proxyInterface, TargetSource targetSource) {
		return (T) new ProxyFactory(proxyInterface, targetSource).getProxy();
	}

	/**
	 * Create a proxy for the specified {@code TargetSource} that extends
	 * the target class of the {@code TargetSource}.
	 * @param targetSource the TargetSource that the proxy should invoke
	 * @return the proxy object
	 */
	public static Object getProxy(TargetSource targetSource) {
		if (targetSource.getTargetClass() == null) {
			throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
		}
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTargetSource(targetSource);
		proxyFactory.setProxyTargetClass(true);
		return proxyFactory.getProxy();
	}

}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Pass a TargetSource that resolves a non-null target class: new SingletonTargetSource(myInstance) or EmptyTargetSource.forClass(MyClass.class).
  2. If you must proxy an interface instead, use ProxyFactory.getProxy(Class<T>, TargetSource).
  3. Verify the bean you wrapped is non-null and its class is obtainable before constructing the TargetSource.

Example fix

// before
TargetSource ts = EmptyTargetSource.INSTANCE; // null class
Object proxy = ProxyFactory.getProxy(ts);

// after
TargetSource ts = EmptyTargetSource.forClass(MyService.class);
Object proxy = ProxyFactory.getProxy(ts);
Defensive patterns

Strategy: validation

Validate before calling

if (targetSource.getTargetClass() == null) {
    throw new IllegalArgumentException("TargetSource must expose a non-null target class: " + targetSource);
}
Object proxy = ProxyFactory.getProxy(targetSource);

Type guard

public static boolean hasResolvableTargetClass(TargetSource ts) {
    return ts != null && ts.getTargetClass() != null;
}

Prevention

When it happens

Trigger: Calling ProxyFactory.getProxy(TargetSource) where the TargetSource was built without a known target class - for example EmptyTargetSource.INSTANCE or a TargetSource whose target is null and whose class was never set.

Common situations: Using a TargetSource backed by a lazy/prototype target that has not yet resolved its class; passing EmptyTargetSource; building a TargetSource from a null target without setting targetClass; misconfiguration in a hot-swappable target source.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/d438748e71a632fe.json. Report an issue: GitHub.