spring-projects/spring-framework · error · IllegalArgumentException

Can't determine type of bean with name '{targetBeanName}'

Error message

Can't determine type of bean with name '{targetBeanName}'

What it means

After targetBeanName is set, setBeanFactory() calls beanFactory.getType(targetBeanName) (line 71) to learn the bean's class so it can resolve the method on it. getType returns null when the bean does not exist or its type cannot be determined (e.g. an unresolvable FactoryBean whose getObjectType() returns null). Spring then throws this IllegalArgumentException.

Source

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

	 * <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 + "]");
		}
	}


	@Override
	public @Nullable Method getObject() throws Exception {
		return this.method;
	}

	@Override
	public Class<Method> getObjectType() {
		return Method.class;

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Verify the bean named by targetBeanName is actually defined in the same BeanFactory (check the bean id and any aliases)
  2. If the target is a FactoryBean, ensure its getObjectType() can return a non-null class early (avoid lazy type resolution)
  3. Confirm you are not splitting MethodLocatingFactoryBean and its target across parent/child contexts where the child cannot resolve the type

Example fix

// before
<bean id="myMethod" class="org.springframework.aop.config.MethodLocatingFactoryBean">
  <property name="targetBeanName" value="misSpelledService"/>
  <property name="methodName" value="doWork"/>
</bean>

// after
<bean id="myMethod" class="org.springframework.aop.config.MethodLocatingFactoryBean">
  <property name="targetBeanName" value="myService"/>
  <property name="methodName" value="doWork"/>
</bean>
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the bean exists and its type is resolvable before defining the factory bean:
if (beanFactory.getBeanNamesForType(...).length == 0 || beanFactory.getType(targetBeanName) == null) {
  throw new IllegalStateException("Bean '" + targetBeanName + "' not resolvable");
}

Type guard

private static boolean beanTypeResolvable(BeanFactory bf, String name) {
  return bf != null && StringUtils.hasText(name) && bf.getType(name) != null;
}

Prevention

When it happens

Trigger: targetBeanName refers to a bean that is not defined in the BeanFactory; the named bean is a FactoryBean whose object type is unknown at this early phase; the bean is in a child context not visible to the one creating the MethodLocatingFactoryBean.

Common situations: Typo in the bean name; the target bean lives in a different (sibling) context; the target is itself a FactoryBean that hasn't been initialized and returns null from getObjectType(); referencing a bean by alias that isn't registered.

Related errors


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