{"record":{"id":"c50cfe47db11599e","repo":"spring-projects/spring-security","slug":"unable-to-create-an-s-bean-expected-one-bean-of","errorCode":null,"errorMessage":"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.","messagePattern":"Unable to create an (.+?) bean\\. Expected one bean of type (.+?), but found multiple\\. Please consider defining only a single bean of this type, or define an (.+?) bean yourself\\.","errorType":"exception","errorClass":"BeanInitializationException","httpStatus":null,"severity":"error","filePath":"config/src/main/java/org/springframework/security/config/http/OAuth2AuthorizedClientManagerRegistrar.java","lineNumber":285,"sourceCode":"\t\treturn additionalAuthorizedClientProviders;\n\t}\n\n\tprivate <T extends OAuth2AuthorizedClientProvider> T getAuthorizedClientProviderByType(\n\t\t\tCollection<OAuth2AuthorizedClientProvider> authorizedClientProviders, Class<T> providerClass) {\n\t\tT authorizedClientProvider = null;\n\t\tfor (OAuth2AuthorizedClientProvider current : authorizedClientProviders) {\n\t\t\tif (providerClass.isInstance(current)) {\n\t\t\t\tassertAuthorizedClientProviderIsNull(authorizedClientProvider);\n\t\t\t\tauthorizedClientProvider = providerClass.cast(current);\n\t\t\t}\n\t\t}\n\t\treturn authorizedClientProvider;\n\t}\n\n\tprivate static void assertAuthorizedClientProviderIsNull(OAuth2AuthorizedClientProvider authorizedClientProvider) {\n\t\tif (authorizedClientProvider != null) {\n\t\t\t// @formatter:off\n\t\t\tthrow new BeanInitializationException(String.format(\n\t\t\t\t\t\"Unable to create an %s bean. Expected one bean of type %s, but found multiple. \" +\n\t\t\t\t\t\"Please consider defining only a single bean of this type, or define an %s bean yourself.\",\n\t\t\t\t\tOAuth2AuthorizedClientManager.class.getName(),\n\t\t\t\t\tauthorizedClientProvider.getClass().getName(),\n\t\t\t\t\tOAuth2AuthorizedClientManager.class.getName()));\n\t\t\t// @formatter:on\n\t\t}\n\t}\n\n\tprivate <T> String[] getBeanNamesForType(Class<T> beanClass) {\n\t\treturn BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, beanClass, false, false);\n\t}\n\n\tprivate <T> T getBeanOfType(ResolvableType resolvableType) {\n\t\tObjectProvider<T> objectProvider = this.beanFactory.getBeanProvider(resolvableType, true);\n\t\treturn objectProvider.getIfAvailable();\n\t}\n","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/config/src/main/java/org/springframework/security/config/http/OAuth2AuthorizedClientManagerRegistrar.java#L267-L303","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove or refactor the authorized-client-provider-ref so only a single OAuth2AuthorizedClientProvider is registered.","Declare your own OAuth2AuthorizedClientManager bean directly instead of relying on the registrar's auto-creation.","Audit imported XML/config fragments for duplicate provider bean definitions and deduplicate them."],"exampleFix":"// before (two provider beans + ref)\n<bean id=\"providerA\" class=\"...ClientCredentialsOAuth2AuthorizedClientProvider\"/>\n<bean id=\"providerB\" class=\"...RefreshTokenOAuth2AuthorizedClientProvider\"/>\n<oauth2-client authorized-client-provider-ref=\"providerA\"/>\n// after (single manager defined by you)\n<bean id=\"authorizedClientManager\" class=\"org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager\">\n  <constructor-arg ref=\"providerA\"/>\n</bean>","handlingStrategy":"validation","validationCode":"int providerCount = ctx.getBeanNamesForType(OAuth2AuthorizedClientProvider.class).length;\nboolean hasRef = xmlElement.hasAttribute(\"authorized-client-provider-ref\");\nif (hasRef && providerCount != 1) { throw new IllegalStateException(\"Ref must yield exactly one OAuth2AuthorizedClientProvider, found \" + providerCount); }","typeGuard":null,"tryCatchPattern":"try { registrar.afterPropertiesSet(); } catch (BeanInitializationException e) { if (e.getMessage().contains(\"found multiple\")) { log.error(\"Duplicate OAuth2AuthorizedClientProvider beans; define OAuth2AuthorizedClientManager yourself\", e); } throw e; }","preventionTips":["Register exactly one OAuth2AuthorizedClientProvider per oauth2-client config","Prefer defining OAuth2AuthorizedClientManager explicitly when using multiple providers","Grep imported XML files for duplicate provider bean definitions"],"tags":["spring-security","bean-initialization","oauth2-client","configuration"],"backgroundTag":"conflicting-config-options","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}