{"id":"4593304aeef5057f","repo":"spring-projects/spring-framework","slug":"specified-parameter-type-is-incompatible-with","errorCode":null,"errorMessage":"Specified parameter type [{}] is incompatible with resource type [{}]","messagePattern":"Specified parameter 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":239,"sourceCode":"\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\n\t\t */\n\t\tprotected boolean shouldInject(@Nullable PropertyValues pvs) {\n\t\t\tif (this.isField) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn !checkPropertySkipping(pvs);\n\t\t}\n","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java#L221-L257","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Align the setter parameter type with the bean Spring resolves (use the shared interface).","Specify the bean name in @Resource(name=...) to force resolution of the correct bean.","If the registered bean type is wrong, fix or remove that bean definition.","Use @Qualifier or @Primary to disambiguate when multiple candidates exist."],"exampleFix":"// before\n@Resource\npublic void setStore(HashMap<String,String> store) { /* bean is a ConcurrentHashMap */ }\n// after\n@Resource(name = \"stringStore\")\npublic void setStore(Map<String,String> store) { ... }","handlingStrategy":"type-guard","validationCode":"Method setter = ...; // the @Resource setter\nClass<?> paramType = setter.getParameterTypes()[0];\nClass<?> beanType = context.getType(beanName);\nif (!(paramType.isAssignableFrom(beanType) || beanType.isAssignableFrom(paramType))) {\n  throw new IllegalStateException(paramType + \" incompatible with \" + beanType);\n}","typeGuard":"boolean setterTypeCompatible(Method setter, Class<?> resourceType) {\n  Class<?> p = setter.getParameterTypes()[0];\n  return p.isAssignableFrom(resourceType) || resourceType.isAssignableFrom(p);\n}","tryCatchPattern":null,"preventionTips":["Type setter injection points with the shared service interface, not the impl class.","Qualify ambiguous @Resource setters with an explicit name.","Refactor renames in lockstep with bean definitions."],"tags":["spring","injection","resource","type-mismatch"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}