{"record":{"id":"d134c031aec6ecee","repo":"spring-projects/spring-security","slug":"no-id-supplied-and-another-bean-is-already-registe","errorCode":null,"errorMessage":"No id supplied and another bean is already registered as {BeanIds.USER_DETAILS_SERVICE}","messagePattern":"No id supplied and another bean is already registered as (.+?)","errorType":"exception","errorClass":"BeanDefinitionStoreException","httpStatus":null,"severity":"error","filePath":"config/src/main/java/org/springframework/security/config/authentication/AbstractUserDetailsServiceBeanDefinitionParser.java","lineNumber":89,"sourceCode":"\tprivate String resolveId(Element element, AbstractBeanDefinition definition, ParserContext pc)\n\t\t\tthrows BeanDefinitionStoreException {\n\t\tString id = element.getAttribute(\"id\");\n\t\tif (pc.isNested()) {\n\t\t\t// We're inside an <authentication-provider> element\n\t\t\tif (!StringUtils.hasText(id)) {\n\t\t\t\tid = pc.getReaderContext().generateBeanName(definition);\n\t\t\t}\n\t\t\tValueHolder userDetailsServiceValueHolder = new ValueHolder(new RuntimeBeanReference(id));\n\t\t\tuserDetailsServiceValueHolder.setName(\"userDetailsService\");\n\t\t\tBeanDefinition container = pc.getContainingBeanDefinition();\n\t\t\tcontainer.getConstructorArgumentValues().addGenericArgumentValue(userDetailsServiceValueHolder);\n\t\t}\n\t\tif (StringUtils.hasText(id)) {\n\t\t\treturn id;\n\t\t}\n\t\t// If top level, use the default name or throw an exception if already used\n\t\tif (pc.getRegistry().containsBeanDefinition(BeanIds.USER_DETAILS_SERVICE)) {\n\t\t\tthrow new BeanDefinitionStoreException(\n\t\t\t\t\t\"No id supplied and another bean is already registered as \" + BeanIds.USER_DETAILS_SERVICE);\n\t\t}\n\t\treturn BeanIds.USER_DETAILS_SERVICE;\n\t}\n\n}\n","sourceCodeStart":71,"sourceCodeEnd":96,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/config/src/main/java/org/springframework/security/config/authentication/AbstractUserDetailsServiceBeanDefinitionParser.java#L71-L96","documentation":"The <user-service> / user details XML parser resolves a bean id for the UserDetailsService. When no explicit 'id' attribute is supplied and the element is top-level, the parser defaults to the reserved id 'userDetailsService' (BeanIds.USER_DETAILS_SERVICE); if a bean with that id is already registered, it throws BeanDefinitionStoreException to prevent a silent overwrite.","triggerScenarios":"Declaring two or more top-level <user-service> (or <jdbc-user-service>, <ldap-user-service>) elements in Spring Security XML namespace config without giving each an 'id' attribute; or a bean named 'userDetailsService' already exists when a top-level user-service element is parsed.","commonSituations":"Legacy XML configs with multiple in-memory user stores (e.g. after a merge of config files); copy-pasting a <user-service> block without an id; combining an explicit UserDetailsService @Bean with XML user-service elements.","solutions":["Give each <user-service> element a unique id attribute, e.g. <user-service id=\"adminUsers\"> and <user-service id=\"appUsers\">.","Remove the duplicate top-level user-service element if only one is needed.","If two UserDetailServices must both be used, wrap them in a delegating implementation and register that as the single userDetailsService bean.","Reference the intended service explicitly with user-service-ref on <authentication-provider> instead of relying on the default id."],"exampleFix":"<!-- before -->\n<user-service>\n  <user name=\"admin\" password=\"{noop}pass\" authorities=\"ROLE_ADMIN\"/>\n</user-service>\n<user-service>\n  <user name=\"user\" password=\"{noop}pass\" authorities=\"ROLE_USER\"/>\n</user-service>\n<!-- after -->\n<user-service id=\"adminUserService\">\n  <user name=\"admin\" password=\"{noop}pass\" authorities=\"ROLE_ADMIN\"/>\n</user-service>\n<user-service id=\"appUserService\">\n  <user name=\"user\" password=\"{noop}pass\" authorities=\"ROLE_USER\"/>\n</user-service>","handlingStrategy":"validation","validationCode":"// Before parsing, verify no duplicate top-level user-service elements and no id collision\nboolean hasUniqueId(List<Element> userServices) {\n    Set<String> ids = new HashSet<>();\n    for (Element el : userServices) {\n        String id = el.getAttribute(\"id\");\n        if (!ids.add(id.isEmpty() ? \"userDetailsService\" : id)) return false;\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n    context = new ClassPathXmlApplicationContext(\"security.xml\");\n} catch (BeanDefinitionStoreException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"USER_DETAILS_SERVICE\")) {\n        throw new IllegalStateException(\"Give each <user-service> a unique id attribute\", e);\n    }\n    throw e;\n}","preventionTips":["Give every top-level <user-service> element an explicit unique id.","Never name application beans 'userDetailsService' unless they are the intended default.","Grep XML configs for multiple <user-service> occurrences after merges.","Prefer referencing services explicitly via user-service-ref."],"tags":["spring-security","xml-config","bean-definition","userdetailsservice"],"backgroundTag":"file-already-exists","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}