{"record":{"id":"a3326572b96c0afc","repo":"spring-projects/spring-framework","slug":"advisor-sorting-failed-with-unexpected-bean-creati","errorCode":null,"errorMessage":"Advisor sorting failed with unexpected bean creation, probably due to custom use of the Ordered interface. Consider using the @Order annotation instead.","messagePattern":"Advisor sorting failed with unexpected bean creation, probably due to custom use of the Ordered interface\\. Consider using the @Order annotation instead\\.","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java","lineNumber":105,"sourceCode":"\t * Find all eligible Advisors for auto-proxying this class.\n\t * @param beanClass the clazz to find advisors for\n\t * @param beanName the name of the currently proxied bean\n\t * @return the empty List, not {@code null},\n\t * if there are no pointcuts or interceptors\n\t * @see #findCandidateAdvisors\n\t * @see #sortAdvisors\n\t * @see #extendAdvisors\n\t */\n\tprotected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {\n\t\tList<Advisor> candidateAdvisors = findCandidateAdvisors();\n\t\tList<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);\n\t\textendAdvisors(eligibleAdvisors);\n\t\tif (!eligibleAdvisors.isEmpty()) {\n\t\t\ttry {\n\t\t\t\teligibleAdvisors = sortAdvisors(eligibleAdvisors);\n\t\t\t}\n\t\t\tcatch (BeanCreationException ex) {\n\t\t\t\tthrow new AopConfigException(\"Advisor sorting failed with unexpected bean creation, probably due \" +\n\t\t\t\t\t\t\"to custom use of the Ordered interface. Consider using the @Order annotation instead.\", ex);\n\t\t\t}\n\t\t}\n\t\treturn eligibleAdvisors;\n\t}\n\n\t/**\n\t * Find all candidate Advisors to use in auto-proxying.\n\t * @return the List of candidate Advisors\n\t */\n\tprotected List<Advisor> findCandidateAdvisors() {\n\t\tAssert.state(this.advisorRetrievalHelper != null, \"No BeanFactoryAdvisorRetrievalHelper available\");\n\t\treturn this.advisorRetrievalHelper.findAdvisorBeans();\n\t}\n\n\t/**\n\t * Search the given candidate Advisors to find all Advisors that\n\t * can apply to the specified bean.","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java#L87-L123","documentation":"Thrown in findEligibleAdvisors (line 104-106) when sorting the eligible Advisor list raises a BeanCreationException. Sorting advisors evaluates their order values, and if an Advisor implements the Ordered interface whose getOrder() triggers eager creation of another (often circular) bean, the resulting BeanCreationException is wrapped in an AopConfigException. The message explicitly recommends @Order annotation over the Ordered interface because @Order is read from metadata without forcing bean instantiation.","triggerScenarios":"An Advisor bean implements Ordered and its getOrder() method accesses another bean (getter call, field that is itself a bean dependency), forcing premature bean creation during advisor sorting and creating a circular-dependency cycle. Common with @Configuration-based advice that injects collaborator beans into fields used inside getOrder().","commonSituations":"Custom AspectJ advice or Advisor annotated to implement Ordered where getOrder() depends on injected state. Spring Boot/Java config with multiple advisors where ordering reads trigger bean creation. Upgrading from a Spring version where ordering was lazier.","solutions":["Replace 'implements Ordered' on the Advisor with '@Order(Ordered.HIGHEST_PRECEDENCE + N)' (or @Order on the advice bean) so ordering is read from annotation metadata, not from a method that needs the bean built.","Ensure getOrder() is a pure constant return with no dependency access.","Break the circular dependency by decoupling the order value from collaborator beans (use a static literal).","Mark the offending dependency @Lazy so it is not eagerly created during getOrder()."],"exampleFix":"// before\n@Component\npublic class MyAdvisor implements Ordered {\n    @Autowired SomeDependency dep; // forces creation during getOrder()\n    public int getOrder() { return dep.priority(); }\n}\n\n// after\n@Component\n@Order(Ordered.HIGHEST_PRECEDENCE + 10)\npublic class MyAdvisor {\n    @Autowired @Lazy SomeDependency dep;\n}","handlingStrategy":"validation","validationCode":"// Order advisors declaratively to avoid bean creation during sorting\n@Component\n@Order(10)\npublic class MyAdvisor { /* no Ordered.getOrder() dependency access */ }","typeGuard":"// Ensure an advisor does not implement Ordered with stateful getOrder()\nstatic boolean safeOrdering(Class<?> advisorType) {\n    return !Ordered.class.isAssignableFrom(advisorType);\n}","tryCatchPattern":null,"preventionTips":["Prefer @Order over implementing Ordered on Advisors.","Keep getOrder() a pure constant; never read injected beans inside it.","Mark cross-cutting dependencies @Lazy to avoid eager creation during sorting."],"tags":["spring-aop","auto-proxy","ordering","circular-dependency"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}