{"record":{"id":"3113f25221ab8cd7","repo":"spring-projects/spring-security","slug":"found-incorrect-number-of-instances-in-applicatio","errorCode":null,"errorMessage":"Found incorrect number of  instances in application context - you must have only have one!","messagePattern":"Found incorrect number of  instances in application context - you must have only have one!","errorType":"exception","errorClass":"JspException","httpStatus":null,"severity":"error","filePath":"taglibs/src/main/java/org/springframework/security/taglibs/authz/AccessControlListTag.java","lineNumber":179,"sourceCode":"\t\t\tSecurityContextHolderStrategy strategy = this.applicationContext\n\t\t\t\t.getBean(SecurityContextHolderStrategy.class);\n\t\t\tthis.securityContextHolderStrategy = strategy;\n\t\t}\n\t}\n\n\tprivate <T> @Nullable T getBeanOfType(Class<T> type) throws JspException {\n\t\tMap<String, T> map = this.applicationContext.getBeansOfType(type);\n\t\tfor (ApplicationContext context = this.applicationContext.getParent(); context != null; context = context\n\t\t\t.getParent()) {\n\t\t\tmap.putAll(context.getBeansOfType(type));\n\t\t}\n\t\tif (map.isEmpty()) {\n\t\t\treturn null;\n\t\t}\n\t\tif (map.size() == 1) {\n\t\t\treturn map.values().iterator().next();\n\t\t}\n\t\tthrow new JspException(\"Found incorrect number of \" + type.getSimpleName() + \" instances in \"\n\t\t\t\t+ \"application context - you must have only have one!\");\n\t}\n\n\tpublic void setDomainObject(Object domainObject) {\n\t\tthis.domainObject = domainObject;\n\t}\n\n\tpublic void setHasPermission(String hasPermission) {\n\t\tthis.hasPermission = hasPermission;\n\t}\n\n\tpublic void setVar(String var) {\n\t\tthis.var = var;\n\t}\n\n}\n","sourceCodeStart":161,"sourceCodeEnd":196,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/taglibs/src/main/java/org/springframework/security/taglibs/authz/AccessControlListTag.java#L161-L196","documentation":"AccessControlListTag.getBeanOfType() looks up exactly one bean of the required ACL type (e.g. AclService or SidRetrievalStrategy) in the application context. If more than one bean of that type exists, the tag cannot decide which to use and throws JspException. Zero beans returns null (deferred to defaults); exactly one is used.","triggerScenarios":"A JSP page using <sec:accesscontrollist> is rendered while the Spring application context contains two or more beans implementing the type required by the ACL tag (typically LookupStrategy/AclService or the SidStrategy), and initializeIfRequired() runs the lookup.","commonSituations":"Defining a primary AclService plus a test/backup one; component scanning pulls in a second AclService bean from a shared library; duplicate bean definitions across XML and @Configuration.","solutions":["Remove or rename the duplicate beans so exactly one bean of the required ACL type exists","Mark the intended bean as @Primary or set autowire-candidate=false on the others (if the lookup honors it, otherwise delete)","Qualify/limit component-scan packages so only one AclService is registered"],"exampleFix":"// before\n@Bean AclService aclService() { ... }\n@Bean AclService backupAclService() { ... } // duplicate\n// after\n@Bean AclService aclService() { ... }\n// (delete or exclude backupAclService)","handlingStrategy":"validation","validationCode":"Map<String, AclService> beans = ctx.getBeansOfType(AclService.class);\nif (beans.size() > 1) throw new IllegalStateException(\n    \"Expected 1 AclService bean, found \" + beans.size() + \": \" + beans.keySet());","typeGuard":null,"tryCatchPattern":"try {\n    pageContext.getAttributes(); // render path\n} catch (JspException e) {\n    if (e.getMessage().contains(\"incorrect number of\")) {\n        log.error(\"Duplicate ACL beans — check getBeansOfType counts\", e);\n    }\n    throw e;\n}","preventionTips":["Keep exactly one bean per ACL service type in the context","Avoid overlapping component-scan packages","Assert bean counts in a Spring context-loading test"],"tags":["spring-security","acl","jsp-taglib","duplicate-bean"],"backgroundTag":"internal-invariant-violation","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}