spring-projects/spring-framework · error · IllegalStateException

Specified parameter type [{}] is incompatible with resource

Error message

Specified parameter type [{}] is incompatible with resource type [{}]

What it means

The setter/method-parameter counterpart of error 203: checkResourceType validates the single parameter type of an annotated setter or arbitrary method injection point against the resolved resource type. If they are not mutually assignable, injection is rejected to prevent a ClassCastException.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java:239

			}
			else {
				return ((Method) this.member).getParameterTypes()[0];
			}
		}

		protected final void checkResourceType(Class<?> resourceType) {
			if (this.isField) {
				Class<?> fieldType = ((Field) this.member).getType();
				if (!(resourceType.isAssignableFrom(fieldType) || fieldType.isAssignableFrom(resourceType))) {
					throw new IllegalStateException("Specified field type [" + fieldType +
							"] is incompatible with resource type [" + resourceType.getName() + "]");
				}
			}
			else {
				Class<?> paramType =
						(this.pd != null ? this.pd.getPropertyType() : ((Method) this.member).getParameterTypes()[0]);
				if (!(resourceType.isAssignableFrom(paramType) || paramType.isAssignableFrom(resourceType))) {
					throw new IllegalStateException("Specified parameter type [" + paramType +
							"] is incompatible with resource type [" + resourceType.getName() + "]");
				}
			}
		}

		/**
		 * Whether the property values should be injected.
		 * @param pvs property values to check
		 * @return whether the property values should be injected
		 * @since 6.0.10
		 */
		protected boolean shouldInject(@Nullable PropertyValues pvs) {
			if (this.isField) {
				return true;
			}
			return !checkPropertySkipping(pvs);
		}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Align the setter parameter type with the bean Spring resolves (use the shared interface).
  2. Specify the bean name in @Resource(name=...) to force resolution of the correct bean.
  3. If the registered bean type is wrong, fix or remove that bean definition.
  4. Use @Qualifier or @Primary to disambiguate when multiple candidates exist.

Example fix

// before
@Resource
public void setStore(HashMap<String,String> store) { /* bean is a ConcurrentHashMap */ }
// after
@Resource(name = "stringStore")
public void setStore(Map<String,String> store) { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

Method setter = ...; // the @Resource setter
Class<?> paramType = setter.getParameterTypes()[0];
Class<?> beanType = context.getType(beanName);
if (!(paramType.isAssignableFrom(beanType) || beanType.isAssignableFrom(paramType))) {
  throw new IllegalStateException(paramType + " incompatible with " + beanType);
}

Type guard

boolean setterTypeCompatible(Method setter, Class<?> resourceType) {
  Class<?> p = setter.getParameterTypes()[0];
  return p.isAssignableFrom(resourceType) || resourceType.isAssignableFrom(p);
}

Prevention

When it happens

Trigger: A method annotated with @Resource whose single parameter type is incompatible with the resolved resource - e.g. @Resource public void setX(SomeService s) but the matching bean is of an unrelated type.

Common situations: Renaming/refactoring a service interface so the setter parameter and the bean class drift apart; @Resource without a name resolving to a different-typed bean; multiple candidate beans where the chosen one has the wrong type.

Related errors


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