{"record":{"id":"7cd99c55b8caf9ba","repo":"spring-projects/spring-framework","slug":"sourcemap-is-required","errorCode":null,"errorMessage":"'sourceMap' is required","messagePattern":"'sourceMap' is required","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/config/MapFactoryBean.java","lineNumber":80,"sourceCode":"\t\t}\n\t\tif (!Map.class.isAssignableFrom(targetMapClass)) {\n\t\t\tthrow new IllegalArgumentException(\"'targetMapClass' must implement [java.util.Map]\");\n\t\t}\n\t\tthis.targetMapClass = targetMapClass;\n\t}\n\n\n\t@Override\n\t@SuppressWarnings(\"rawtypes\")\n\tpublic Class<Map> getObjectType() {\n\t\treturn Map.class;\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tprotected Map<Object, Object> createInstance() {\n\t\tif (this.sourceMap == null) {\n\t\t\tthrow new IllegalArgumentException(\"'sourceMap' is required\");\n\t\t}\n\t\tMap<Object, Object> result = null;\n\t\tif (this.targetMapClass != null) {\n\t\t\tresult = BeanUtils.instantiateClass(this.targetMapClass);\n\t\t}\n\t\telse {\n\t\t\tresult = CollectionUtils.newLinkedHashMap(this.sourceMap.size());\n\t\t}\n\t\tClass<?> keyType = null;\n\t\tClass<?> valueType = null;\n\t\tif (this.targetMapClass != null) {\n\t\t\tResolvableType mapType = ResolvableType.forClass(this.targetMapClass).asMap();\n\t\t\tkeyType = mapType.resolveGeneric(0);\n\t\t\tvalueType = mapType.resolveGeneric(1);\n\t\t}\n\t\tif (keyType != null || valueType != null) {\n\t\t\tTypeConverter converter = getBeanTypeConverter();\n\t\t\tfor (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/config/MapFactoryBean.java#L62-L98","documentation":"Thrown by MapFactoryBean.createInstance() when sourceMap was never provided. MapFactoryBean has no sensible default content, so a missing source map is a configuration error. It is raised at bean-creation time when the Map is first requested from the factory.","triggerScenarios":"A MapFactoryBean bean definition lacking <property name=\"sourceMap\">. Programmatic use without calling setSourceMap. A <util:map> where the inner <map> element was dropped.","commonSituations":"Intending an empty map but omitting the property. Refactoring that removed the sourceMap element. Misunderstanding that MapFactoryBean requires a populated source even when empty.","solutions":["Add a source map: <property name=\"sourceMap\"><map>...</map></property> or fb.setSourceMap(Map.of(...)).","For an empty map, pass an explicit empty map: fb.setSourceMap(new HashMap<>()) or <property name=\"sourceMap\"><map/></property>.","If sharing isn't needed, inject a plain Map field directly rather than using MapFactoryBean."],"exampleFix":"// before (broken)\nMapFactoryBean fb = new MapFactoryBean();\nfb.getObject();\n// after\nMapFactoryBean fb = new MapFactoryBean();\nfb.setSourceMap(Map.of(\"k\", \"v\"));","handlingStrategy":"validation","validationCode":"if (sourceMap == null) {\n    sourceMap = Map.of(); // explicit empty, or fail fast\n}\nfactoryBean.setSourceMap(sourceMap);","typeGuard":"static boolean hasSourceMap(MapFactoryBean fb) {\n    // no public getter for sourceMap; enforce at the call site\n    return true; // ensure setSourceMap was called before getObject()\n}","tryCatchPattern":"try {\n    return fb.getObject();\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().contains(\"sourceMap\")) {\n        fb.setSourceMap(Map.of());\n        return fb.getObject();\n    }\n    throw ex;\n}","preventionTips":["Always call setSourceMap, even with Map.of(), when using MapFactoryBean.","Prefer <util:map> in XML which requires inner entries.","Cover the bean in a context-load smoke test."],"tags":["spring","factory-bean","collection","missing-property","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}