{"id":"aed95be2249245dd","repo":"spring-projects/spring-framework","slug":"specified-field-type-is-incompatible-with-res","errorCode":null,"errorMessage":"Specified field type [{}] is incompatible with resource type [{}]","messagePattern":"Specified field type \\[(.+?)\\] is incompatible with resource type \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java","lineNumber":231,"sourceCode":"\t\t}\n\n\t\tprotected final Class<?> getResourceType() {\n\t\t\tif (this.isField) {\n\t\t\t\treturn ((Field) this.member).getType();\n\t\t\t}\n\t\t\telse if (this.pd != null) {\n\t\t\t\treturn this.pd.getPropertyType();\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn ((Method) this.member).getParameterTypes()[0];\n\t\t\t}\n\t\t}\n\n\t\tprotected final void checkResourceType(Class<?> resourceType) {\n\t\t\tif (this.isField) {\n\t\t\t\tClass<?> fieldType = ((Field) this.member).getType();\n\t\t\t\tif (!(resourceType.isAssignableFrom(fieldType) || fieldType.isAssignableFrom(resourceType))) {\n\t\t\t\t\tthrow new IllegalStateException(\"Specified field type [\" + fieldType +\n\t\t\t\t\t\t\t\"] is incompatible with resource type [\" + resourceType.getName() + \"]\");\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tClass<?> paramType =\n\t\t\t\t\t\t(this.pd != null ? this.pd.getPropertyType() : ((Method) this.member).getParameterTypes()[0]);\n\t\t\t\tif (!(resourceType.isAssignableFrom(paramType) || paramType.isAssignableFrom(resourceType))) {\n\t\t\t\t\tthrow new IllegalStateException(\"Specified parameter type [\" + paramType +\n\t\t\t\t\t\t\t\"] is incompatible with resource type [\" + resourceType.getName() + \"]\");\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Whether the property values should be injected.\n\t\t * @param pvs property values to check\n\t\t * @return whether the property values should be injected\n\t\t * @since 6.0.10","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java#L213-L249","documentation":"During injection metadata building, InjectionMetadata.InjectedElement.checkResourceType verifies that a @Resource/@Inject target field's declared type is assignment-compatible with the resolved resource type. If neither type is assignable to the other, Spring refuses to inject because the value would not be type-safe. This guards against silent ClassCastException at injection time.","triggerScenarios":"A field annotated with @Resource (or processed by CommonAnnotationBeanPostProcessor) whose declared type is incompatible with the resource/bean Spring resolves for that name or type. For example @Resource private DataSource field but the matching bean is a String.","commonSituations":"Wrong bean name resolved (name collision), a registered bean of an unexpected type shadowing the intended one, a field type that was changed without updating the wiring, or mixing interface/implementation types where neither is assignable to the other (e.g. two unrelated hierarchies).","solutions":["Verify the resolved bean's actual type matches (or is assignable to/from) the field type - check the bean name in @Resource and the registered bean definitions.","If a name collision resolves the wrong bean, qualify explicitly with the correct bean name or mark it @Primary.","Update the field type to the implementation/interface that Spring actually provides, or register the correct type.","Remove the stale bean definition that is shadowing the intended one."],"exampleFix":"// before\n@Resource\nprivate DataSource cache; // 'cache' bean is actually a Caffeine Map\n// after\n@Resource(name = \"dataSource\")\nprivate DataSource cache;","handlingStrategy":"type-guard","validationCode":"Field f = ...; // the @Resource field\nClass<?> beanType = context.getType(beanName);\nClass<?> fieldType = f.getType();\nif (!(fieldType.isAssignableFrom(beanType) || beanType.isAssignableFrom(fieldType))) {\n  throw new IllegalStateException(fieldType + \" incompatible with \" + beanType);\n}","typeGuard":"boolean typesCompatible(Class<?> fieldType, Class<?> resourceType) {\n  return fieldType.isAssignableFrom(resourceType) || resourceType.isAssignableFrom(fieldType);\n}","tryCatchPattern":null,"preventionTips":["Prefer constructor injection over @Resource to surface type mismatches at compile time.","Always specify @Resource(name=...) when multiple beans of related types exist.","Keep field types as shared interfaces shared by the bean and the consumer."],"tags":["spring","injection","resource","type-mismatch"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}