spring-projects/spring-framework · error · IllegalArgumentException

'target' needs to be a bean reference, not a bean name as…

Error message

'target' needs to be a bean reference, not a bean name as value

What it means

AbstractSingletonProxyFactoryBean.afterPropertiesSet() checks (line 140) whether target is a String and rejects it: the target must be the actual bean instance (a reference), not the bean's name. This catches the classic XML mistake of using value= (a literal string) instead of ref= (a bean reference).

Solutions

  1. In XML, change <property name="target" value="myService"/> to <property name="target" ref="myService"/>
  2. Programmatically, fetch the bean instance first (context.getBean("myService")) and pass that to setTarget(...), never the name string
  3. Review all target= attributes on proxy factory beans to confirm they use ref semantics

Example fix

// before
<property name="target" value="myService"/>

// after
<property name="target" ref="myService"/>
Defensive patterns

Strategy: type-guard

Validate before calling

Object t = factoryBean.getTarget();
if (t instanceof String) {
  throw new IllegalStateException("target must be a bean reference, not the name: " + t);
}

Type guard

private static boolean targetIsReference(AbstractSingletonProxyFactoryBean fb) {
  Object t = (fb == null) ? null : fb.getTarget();
  return t != null && !(t instanceof String);
}

Prevention

When it happens

Trigger: Setting target to a String value - e.g. XML <property name="target" value="myService"/> (value, not ref) - or programmatically calling setTarget("myService") passing the bean name instead of the bean object.

Common situations: XML config using value="beanName" where ref="beanName" was intended; programmatic wiring that passes a bean name string by mistake; copy-paste from a property that genuinely takes a string.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/76caa402b4ef38b6. Report an issue: GitHub.

Appendix: source

Thrown at spring-aop/src/main/java/org/springframework/aop/framework/AbstractSingletonProxyFactoryBean.java:141

	public void setProxyClassLoader(ClassLoader classLoader) {
		this.proxyClassLoader = classLoader;
	}

	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		if (this.proxyClassLoader == null) {
			this.proxyClassLoader = classLoader;
		}
	}


	@Override
	public void afterPropertiesSet() {
		if (this.target == null) {
			throw new IllegalArgumentException("Property 'target' is required");
		}
		if (this.target instanceof String) {
			throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
		}
		if (this.proxyClassLoader == null) {
			this.proxyClassLoader = ClassUtils.getDefaultClassLoader();
		}

		ProxyFactory proxyFactory = new ProxyFactory();

		if (this.preInterceptors != null) {
			for (Object interceptor : this.preInterceptors) {
				proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
			}
		}

		// Add the main interceptor (typically an Advisor).
		proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));

		if (this.postInterceptors != null) {
			for (Object interceptor : this.postInterceptors) {

View on GitHub (pinned to 69bf83ad71)