spring-projects/spring-framework · error · IllegalArgumentException

Argument type mismatch: expected '{}' for value [{}]

Error message

Argument type mismatch: expected '{}' for value [{}]

What it means

AutowiredArguments.get(index, requiredType) is used by AOT-generated BeanInstanceSupplier code to fetch a resolved autowired argument and cast it to the type the generated code expects. If the runtime-resolved value is not assignable to requiredType (validated via ClassUtils.isAssignableValue), an IllegalArgumentException is thrown - a sign that the AOT-time argument shape disagrees with runtime resolution.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArguments.java:47

 * @since 6.0
 * @see BeanInstanceSupplier
 * @see AutowiredMethodArgumentsResolver
 */
@FunctionalInterface
public interface AutowiredArguments {

	/**
	 * Return the resolved argument at the specified index.
	 * @param <T> the type of the argument
	 * @param index the argument index
	 * @param requiredType the required argument type
	 * @return the argument
	 */
	@SuppressWarnings("unchecked")
	default <T> @Nullable T get(int index, Class<T> requiredType) {
		Object value = getObject(index);
		if (!ClassUtils.isAssignableValue(requiredType, value)) {
			throw new IllegalArgumentException("Argument type mismatch: expected '" +
					requiredType.getTypeName() + "' for value [" + value + "]");
		}
		return (T) value;
	}

	/**
	 * Return the resolved argument at the specified index.
	 * @param <T> the type of the argument
	 * @param index the argument index
	 * @return the argument
	 */
	@SuppressWarnings("unchecked")
	default <T> @Nullable T get(int index) {
		return (T) getObject(index);
	}

	/**
	 * Return the resolved argument at the specified index.

View on GitHub (pinned to e8729d0438)

Solutions

  1. Regenerate the AOT artifacts (spring-boot-maven-plugin process-aot / native build) after any bean/dependency change so captured argument types match runtime.
  2. Ensure the runtime classpath is identical to the AOT classpath (no stale or overridden beans).
  3. Check that the bean supplying the argument is of the expected type - fix the conflicting bean definition.

Example fix

// before: AOT generated for ctor(Service s) but runtime bean is now LegacyService
// after: rebuild AOT artifacts and align runtime classpath
// mvn -Pnative spring-boot:process-aot
Defensive patterns

Strategy: validation

Validate before calling

for (int i = 0; i < args.toArray().length; i++) {
  Object v = args.getObject(i);
  Class<?> expected = executable.getParameters()[i].getType();
  if (!ClassUtils.isAssignableValue(expected, v)) {
    throw new IllegalStateException("arg " + i + " " + (v==null?"null":v.getClass()) + " !<= " + expected);
  }
}

Type guard

boolean argCompatible(Class<?> expected, @Nullable Object value) {
  return ClassUtils.isAssignableValue(expected, value);
}

Prevention

When it happens

Trigger: AOT processing captured the constructor/factory-method parameter types, but at runtime the bean factory resolved an argument whose type no longer matches (different bean implementation, widened type, null where a primitive is required).

Common situations: Running AOT-optimized code against a classpath that differs from build time (bean implementation swapped), a bean returning a subtype that widened during a dependency upgrade, or a configuration where the autowired candidate changed type between AOT and runtime.

Related errors


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