spring-projects/spring-security · error · BeanInitializationException

Unable to create an %s bean. Expected one bean of type %s, b

Error message

Unable to create an %s bean. Expected one bean of type %s, but found multiple. Please consider defining only a single bean of this type, or define an %s bean yourself.

What it means

When using the <oauth2-client> XML namespace element, Spring Security tries to build an OAuth2AuthorizedClientManager bean automatically. If you have supplied a custom authorized-client-provider-ref that resolves to a bean of a type already registered elsewhere, the registrar detects the conflict and refuses to initialize the bean, throwing BeanInitializationException via assertAuthorizedClientProviderIsNull (OAuth2AuthorizedClientManagerRegistrar.java:285). The library throws this because a manager can only be auto-assembled when exactly one OAuth2AuthorizedClientProvider governs it; with multiple providers it cannot infer intended behavior.

Source

Thrown at config/src/main/java/org/springframework/security/config/http/OAuth2AuthorizedClientManagerRegistrar.java:285

		return additionalAuthorizedClientProviders;
	}

	private <T extends OAuth2AuthorizedClientProvider> T getAuthorizedClientProviderByType(
			Collection<OAuth2AuthorizedClientProvider> authorizedClientProviders, Class<T> providerClass) {
		T authorizedClientProvider = null;
		for (OAuth2AuthorizedClientProvider current : authorizedClientProviders) {
			if (providerClass.isInstance(current)) {
				assertAuthorizedClientProviderIsNull(authorizedClientProvider);
				authorizedClientProvider = providerClass.cast(current);
			}
		}
		return authorizedClientProvider;
	}

	private static void assertAuthorizedClientProviderIsNull(OAuth2AuthorizedClientProvider authorizedClientProvider) {
		if (authorizedClientProvider != null) {
			// @formatter:off
			throw new BeanInitializationException(String.format(
					"Unable to create an %s bean. Expected one bean of type %s, but found multiple. " +
					"Please consider defining only a single bean of this type, or define an %s bean yourself.",
					OAuth2AuthorizedClientManager.class.getName(),
					authorizedClientProvider.getClass().getName(),
					OAuth2AuthorizedClientManager.class.getName()));
			// @formatter:on
		}
	}

	private <T> String[] getBeanNamesForType(Class<T> beanClass) {
		return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, beanClass, false, false);
	}

	private <T> T getBeanOfType(ResolvableType resolvableType) {
		ObjectProvider<T> objectProvider = this.beanFactory.getBeanProvider(resolvableType, true);
		return objectProvider.getIfAvailable();
	}

View on GitHub (pinned to 96852e8860)

Solutions

  1. Remove or refactor the authorized-client-provider-ref so only a single OAuth2AuthorizedClientProvider is registered.
  2. Declare your own OAuth2AuthorizedClientManager bean directly instead of relying on the registrar's auto-creation.
  3. Audit imported XML/config fragments for duplicate provider bean definitions and deduplicate them.

Example fix

// before (two provider beans + ref)
<bean id="providerA" class="...ClientCredentialsOAuth2AuthorizedClientProvider"/>
<bean id="providerB" class="...RefreshTokenOAuth2AuthorizedClientProvider"/>
<oauth2-client authorized-client-provider-ref="providerA"/>
// after (single manager defined by you)
<bean id="authorizedClientManager" class="org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager">
  <constructor-arg ref="providerA"/>
</bean>
Defensive patterns

Strategy: validation

Validate before calling

int providerCount = ctx.getBeanNamesForType(OAuth2AuthorizedClientProvider.class).length;
boolean hasRef = xmlElement.hasAttribute("authorized-client-provider-ref");
if (hasRef && providerCount != 1) { throw new IllegalStateException("Ref must yield exactly one OAuth2AuthorizedClientProvider, found " + providerCount); }

Try / catch

try { registrar.afterPropertiesSet(); } catch (BeanInitializationException e) { if (e.getMessage().contains("found multiple")) { log.error("Duplicate OAuth2AuthorizedClientProvider beans; define OAuth2AuthorizedClientManager yourself", e); } throw e; }

Prevention

When it happens

Trigger: Using <oauth2-client> XML config with an authorized-client-provider-ref pointing at a provider bean while the registrar also discovers multiple OAuth2AuthorizedClientProvider beans of type authorizedClientProvider in the context, so the assert sees authorizedClientProvider != null when it must be null.

Common situations: Applications migrating from Boot auto-config to XML security namespace; teams defining both a custom composite provider and another provider bean; duplicate bean definitions across imported XML files.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10). Data as JSON: /api/errors/c50cfe47db11599e. Report an issue: GitHub.