spring-projects/spring-framework · error · IllegalStateException

Specified parameter type [

Error message

Specified parameter type [

What it means

Thrown by InjectionMetadata.InjectedElement.checkResourceType() when the parameter type of an injected method or property setter is incompatible with the resolved resource type — checked at line 238. This is the method/setter counterpart of error 216 which covers fields.

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 69bf83ad71)

Solutions

  1. Ensure the setter/method parameter type is compatible with the actual bean type (assignable in at least one direction).
  2. If using @Resource by name, verify the named bean's type matches the parameter type.
  3. Update the parameter type or switch to a compatible bean.

Example fix

// before
@Resource(name = "emailService")
public void setService(SmsService sms) { ... } // type mismatch

// after
@Resource(name = "emailService")
public void setService(EmailService email) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// Verify setter parameter type compatibility before injection
for (Method m : beanClass.getDeclaredMethods()) {
    if (m.isAnnotationPresent(Resource.class) && m.getParameterCount() == 1) {
        Resource r = m.getAnnotation(Resource.class);
        Class<?> paramType = m.getParameterTypes()[0];
        if (!r.name().isEmpty() && beanFactory.containsBean(r.name())) {
            Class<?> beanType = beanFactory.getType(r.name());
            if (beanType != null
                    && !paramType.isAssignableFrom(beanType)
                    && !beanType.isAssignableFrom(paramType)) {
                throw new IllegalStateException(
                    "Setter param type " + paramType + " incompatible with bean " + r.name());
            }
        }
    }
}

Prevention

When it happens

Trigger: A setter method or property annotated for injection has a parameter whose type is incompatible with the resolved bean — neither assignable in either direction. For example, @Resource on setFoo(Bar bar) where the named resource is of type Baz.

Common situations: @Resource(name=...) on a setter where the named bean's type differs from the parameter type. Changing a setter parameter type without updating the bean type. Generic type erasure causing assignability failure at runtime.

Related errors


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