{"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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java#L87-L123","documentation":"sortAdvisors() uses AnnotationAwareOrderComparator which may consult the Ordered interface, and resolving an Ordered value can trigger eager bean creation. If that happens while advisors are still being assembled (a circular reference), BeanCreationException is caught and rethrown as AopConfigException with this guidance, pointing users toward @Order (which is read from metadata without instantiating the bean).","triggerScenarios":"An advisor/advice bean implements org.springframework.core.Ordered whose getOrder() method accesses another still-being-created bean; ordering advisors during findEligibleAdvisors() then forces premature instantiation, creating a cycle.","commonSituations":"Custom advisors implementing Ordered with logic that depends on other beans; upgrading code that previously used @Order to use the Ordered interface incorrectly; cross-bean dependencies surfaced only at advisor-sort time.","solutions":["Replace 'implements Ordered' on advisors with the @Order annotation (or @Order on a @Bean method) so ordering is read from metadata without bean creation.","Decouple getOrder() from any bean lookups - return a constant.","Break the circular dependency that the Ordered implementation triggers."],"exampleFix":"// before\npublic class MyAspect implements Ordered {\n  public int getOrder() { return depBean.getPriority(); } // triggers creation\n}\n\n// after\n@Order(5)\npublic class MyAspect { ... }","handlingStrategy":"validation","validationCode":"// Detect advisors implementing Ordered whose getOrder may trigger bean creation\nfor (Advisor a : candidateAdvisors) {\n    if (a instanceof org.springframework.core.Ordered\n            && a.getClass().isAnnotationPresent(org.springframework.core.annotation.Order.class) == false) {\n        log.warn(\"Advisor {} implements Ordered; prefer @Order to avoid eager bean creation during sort\", a.getClass());\n}","typeGuard":"public static boolean usesOrderedInterface(Advisor a) {\n    return a instanceof org.springframework.core.Ordered;\n}","tryCatchPattern":"try {\n    return autoProxyCreator.postProcessAfterInitialization(bean, beanName);\n} catch (AopConfigException ex) {\n    if (ex.getMessage().contains(\"Advisor sorting failed\")) {\n        log.error(\"Convert Ordered advisors to @Order to break circular bean creation\", ex);\n    }\n    throw ex;\n}","preventionTips":["Annotate advisors with @Order instead of implementing Ordered.","Keep any getOrder() implementation free of bean lookups.","Break circular dependencies surfaced at advisor-sort time."],"tags":["aop","auto-proxy","ordering","circular-dependency"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}