{"record":{"id":"70509846bb72d6db","repo":"spring-projects/spring-framework","slug":"specified-field-type","errorCode":null,"errorMessage":"Specified field type [","messagePattern":"Specified field 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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java#L213-L249","documentation":"Thrown by InjectionMetadata.InjectedElement.checkResourceType() when the declared type of an @Autowired/@Resource field is incompatible with the resolved resource type — neither type is assignable to the other in either direction (checked at line 230). This catches type mismatches between what is declared on the injection point and what Spring resolves to inject.","triggerScenarios":"A field annotated for injection declares a type that is neither a supertype nor subtype of the resolved bean type. For example, a field of type Foo is resolved to a bean of type Bar where neither extends the other.","commonSituations":"Using @Resource(name=\"...\") to inject a bean whose type does not match the field. Changing a bean's implementation class without updating the injection point type. Injecting by name where the named bean's type diverges from the field type. Generic type erasure causing a runtime type mismatch.","solutions":["Ensure the field type is compatible (assignable) with the actual bean type — either the same type, a supertype, or a subtype.","If injecting by name with @Resource, verify the named bean's class matches or is assignable to the field type.","If the type hierarchy changed, update the injection point to use the correct common interface or type."],"exampleFix":"// before\n@Resource(name = \"stringCache\")\nprivate IntegerCache cache; // stringCache is a StringCache, incompatible\n\n// after\n@Resource(name = \"stringCache\")\nprivate StringCache cache; // types now match","handlingStrategy":"validation","validationCode":"// Verify field type compatibility before injection\nfor (Field f : beanClass.getDeclaredFields()) {\n    if (f.isAnnotationPresent(Resource.class)) {\n        Resource r = f.getAnnotation(Resource.class);\n        if (!r.name().isEmpty() && beanFactory.containsBean(r.name())) {\n            Class<?> beanType = beanFactory.getType(r.name());\n            if (beanType != null\n                    && !f.getType().isAssignableFrom(beanType)\n                    && !beanType.isAssignableFrom(f.getType())) {\n                throw new IllegalStateException(\n                    \"Field type \" + f.getType() + \" incompatible with bean \" + r.name());\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure @Resource field types match or are supertypes of the named bean's class.","Update injection point types when changing bean implementation classes.","Use @Autowired (type-driven) instead of @Resource by name to get compile-time type checking."],"tags":["spring","injection","type-mismatch","resource"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}