spring-projects/spring-framework · error · IllegalArgumentException

Property 'targetBeanName' is required

Error message

Property 'targetBeanName' is required

What it means

Thrown by MethodLocatingFactoryBean.setBeanFactory() when the 'targetBeanName' property has not been set (blank or null). MethodLocatingFactoryBean is an internal FactoryBean used by the <aop:config> parser to locate an advice method on an aspect bean; it needs both the bean name and the method name to resolve the Method reflectively. A blank targetBeanName means the parser could not determine which aspect bean owns the advice method.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java:65

	 * @param targetBeanName the name of the bean to locate the {@link Method} on
	 */
	public void setTargetBeanName(String targetBeanName) {
		this.targetBeanName = targetBeanName;
	}

	/**
	 * Set the name of the {@link Method} to locate.
	 * <p>This property is required.
	 * @param methodName the name of the {@link Method} to locate
	 */
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) {
		if (!StringUtils.hasText(this.targetBeanName)) {
			throw new IllegalArgumentException("Property 'targetBeanName' is required");
		}
		if (!StringUtils.hasText(this.methodName)) {
			throw new IllegalArgumentException("Property 'methodName' is required");
		}

		Class<?> beanClass = beanFactory.getType(this.targetBeanName);
		if (beanClass == null) {
			throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'");
		}
		this.method = BeanUtils.resolveSignature(this.methodName, beanClass);

		if (this.method == null) {
			throw new IllegalArgumentException("Unable to locate method [" + this.methodName +
					"] on bean [" + this.targetBeanName + "]");
		}
	}

View on GitHub (pinned to e8729d0438)

Solutions

  1. In XML, ensure each <aop:aspect> that declares advice has a non-empty 'ref' attribute pointing to the aspect bean.
  2. When constructing MethodLocatingFactoryBean programmatically, always call setTargetBeanName(beanName) before the bean is initialized.
  3. Validate the <aop:config> block against the Spring AOP schema to catch missing attributes early.

Example fix

<!-- before -->
<aop:config>
  <aop:aspect id="auditAspect">
    <aop:before method="log" pointcut="execution(* *(..))"/>
  </aop:aspect>
</aop:config>
<!-- after -->
<aop:config>
  <aop:aspect id="auditAspect" ref="auditBean">
    <aop:before method="log" pointcut="execution(* *(..))"/>
  </aop:aspect>
</aop:config>
Defensive patterns

Strategy: validation

Validate before calling

import org.w3c.dom.Element;

void ensureAspectRefSet(Element aspectElement) {
  String ref = aspectElement.getAttribute("ref");
  if (ref == null || ref.isBlank())
    throw new IllegalArgumentException("<aop:aspect> declaring advice must set a non-empty 'ref' attribute");
}

Prevention

When it happens

Trigger: Spring internally builds a MethodLocatingFactoryBean bean definition while parsing <aop:before method="..."/> inside <aop:aspect>. The targetBeanName is derived from the <aspect ref="..."> attribute; if the aspect ref is blank/missing in a context where an advice node is present, or the bean definition is constructed incorrectly, targetBeanName stays blank and setBeanFactory throws.

Common situations: An <aop:aspect> element declares advice (<aop:before> etc.) but omits or leaves blank the 'ref' attribute — normally the parser pre-empts this with its own error, but edge cases (custom namespace extension, programmatic bean definition construction) can reach this guard. Programmatic misuse of MethodLocatingFactoryBean without calling setTargetBeanName.

Related errors


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