spring-projects/spring-security · error · BeanDefinitionStoreException
No id supplied and another bean is already registered as {Be
Error message
No id supplied and another bean is already registered as {BeanIds.USER_DETAILS_SERVICE} What it means
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.
Source
Thrown at config/src/main/java/org/springframework/security/config/authentication/AbstractUserDetailsServiceBeanDefinitionParser.java:89
private String resolveId(Element element, AbstractBeanDefinition definition, ParserContext pc)
throws BeanDefinitionStoreException {
String id = element.getAttribute("id");
if (pc.isNested()) {
// We're inside an <authentication-provider> element
if (!StringUtils.hasText(id)) {
id = pc.getReaderContext().generateBeanName(definition);
}
ValueHolder userDetailsServiceValueHolder = new ValueHolder(new RuntimeBeanReference(id));
userDetailsServiceValueHolder.setName("userDetailsService");
BeanDefinition container = pc.getContainingBeanDefinition();
container.getConstructorArgumentValues().addGenericArgumentValue(userDetailsServiceValueHolder);
}
if (StringUtils.hasText(id)) {
return id;
}
// If top level, use the default name or throw an exception if already used
if (pc.getRegistry().containsBeanDefinition(BeanIds.USER_DETAILS_SERVICE)) {
throw new BeanDefinitionStoreException(
"No id supplied and another bean is already registered as " + BeanIds.USER_DETAILS_SERVICE);
}
return BeanIds.USER_DETAILS_SERVICE;
}
}
View on GitHub (pinned to 96852e8860)
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.
Example fix
<!-- before -->
<user-service>
<user name="admin" password="{noop}pass" authorities="ROLE_ADMIN"/>
</user-service>
<user-service>
<user name="user" password="{noop}pass" authorities="ROLE_USER"/>
</user-service>
<!-- after -->
<user-service id="adminUserService">
<user name="admin" password="{noop}pass" authorities="ROLE_ADMIN"/>
</user-service>
<user-service id="appUserService">
<user name="user" password="{noop}pass" authorities="ROLE_USER"/>
</user-service> Defensive patterns
Strategy: validation
Validate before calling
// Before parsing, verify no duplicate top-level user-service elements and no id collision
boolean hasUniqueId(List<Element> userServices) {
Set<String> ids = new HashSet<>();
for (Element el : userServices) {
String id = el.getAttribute("id");
if (!ids.add(id.isEmpty() ? "userDetailsService" : id)) return false;
}
return true;
} Try / catch
try {
context = new ClassPathXmlApplicationContext("security.xml");
} catch (BeanDefinitionStoreException e) {
if (e.getMessage() != null && e.getMessage().contains("USER_DETAILS_SERVICE")) {
throw new IllegalStateException("Give each <user-service> a unique id attribute", e);
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Use of a properties file and user elements are mutually excl
- You must supply user definitions, either with <user> child e
- Did you forget to add a global <authentication-manager> elem
- Unknown channel attribute {requiredChannel}
- Could not create CorsFilter
AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10).
Data as JSON: /api/errors/d134c031aec6ecee.
Report an issue: GitHub.