{"record":{"id":"aeed897f49da9d99","repo":"alibaba/spring-ai-alibaba","slug":"strategy-type-is-already-registered","errorCode":null,"errorMessage":"Strategy type '' is already registered","messagePattern":"Strategy type '' is already registered","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/strategy/FlowGraphBuildingStrategyRegistry.java","lineNumber":69,"sourceCode":"\t}\n\n\t/**\n\t * Registers a new graph building strategy (same instance returned each time).\n\t * @param strategy the strategy to register\n\t * @throws IllegalArgumentException if strategy is null or type is already registered\n\t */\n\tpublic void registerStrategy(FlowGraphBuildingStrategy strategy) {\n\t\tif (strategy == null) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy cannot be null\");\n\t\t}\n\n\t\tString type = strategy.getStrategyType();\n\t\tif (type == null || type.trim().isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy type cannot be null or empty\");\n\t\t}\n\n\t\tif (strategyFactories.containsKey(type)) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy type '\" + type + \"' is already registered\");\n\t\t}\n\n\t\tstrategyFactories.put(type, () -> strategy);\n\t}\n\n\t/**\n\t * Registers a strategy factory. Each call to {@link #createStrategy(String)} or\n\t * {@link #getStrategy(String)} will use the factory to obtain a strategy instance.\n\t * @param type the strategy type\n\t * @param factory the factory that creates strategy instances\n\t * @throws IllegalArgumentException if type or factory is null, or type is already registered\n\t */\n\tpublic void registerStrategy(String type, Supplier<FlowGraphBuildingStrategy> factory) {\n\t\tif (type == null || type.trim().isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy type cannot be null or empty\");\n\t\t}\n\t\tif (factory == null) {\n\t\t\tthrow new IllegalArgumentException(\"Strategy factory cannot be null\");","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/strategy/FlowGraphBuildingStrategyRegistry.java#L51-L87","documentation":"Thrown by FlowGraphBuildingStrategyRegistry.registerStrategy(FlowGraphBuildingStrategy) when the strategy's getStrategyType() returns a type string that was already registered in the registry. The registry enforces one factory per type key so that createStrategy(type) is unambiguous. Duplicate registration almost always means the same strategy instance (or two strategies returning the same type string) is being registered twice, e.g. during repeated initialization or hot re-registration.","triggerScenarios":"Calling registerStrategy(strategy) where strategy.getStrategyType() equals a type already present in strategyFactories; re-running an initialization block that registers built-in strategies without clearing the registry; two custom strategies accidentally sharing the same strategyType string.","commonSituations":"Spring bean re-creation or application context refresh invoking registry setup twice; copy-pasting a custom strategy class and forgetting to change the returned strategy type; tests constructing a fresh registry per case but reusing a singleton strategy.","solutions":["Do not re-register an existing type; check registry.containsStrategy(type) (or try createStrategy) before registering.","Use the registerStrategy(String type, Supplier) overload only for genuinely new type strings.","Ensure initialization code runs once (guard with a boolean, @PostConstruct, or singleton registry).","If replacement is intended, add/use an explicit unregister or replace API instead of throwing on duplicate registration."],"exampleFix":"// before\nregistry.registerStrategy(myStrategy); // myStrategy.getStrategyType() == \"loop\" (already registered)\n// after\nif (!registry.containsStrategy(myStrategy.getStrategyType())) {\n    registry.registerStrategy(myStrategy);\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["java","illegal-argument","registry","duplicate-registration"],"backgroundTag":"duplicate-key-registration","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}