spring-projects/spring-framework · error · AopConfigException
Advisor sorting failed with unexpected bean creation, probab
Error message
Advisor sorting failed with unexpected bean creation, probably due to custom use of the Ordered interface. Consider using the @Order annotation instead.
What it means
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).
Source
Thrown at spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java:105
* Find all eligible Advisors for auto-proxying this class.
* @param beanClass the clazz to find advisors for
* @param beanName the name of the currently proxied bean
* @return the empty List, not {@code null},
* if there are no pointcuts or interceptors
* @see #findCandidateAdvisors
* @see #sortAdvisors
* @see #extendAdvisors
*/
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
List<Advisor> candidateAdvisors = findCandidateAdvisors();
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty()) {
try {
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
}
catch (BeanCreationException ex) {
throw new AopConfigException("Advisor sorting failed with unexpected bean creation, probably due " +
"to custom use of the Ordered interface. Consider using the @Order annotation instead.", ex);
}
}
return eligibleAdvisors;
}
/**
* Find all candidate Advisors to use in auto-proxying.
* @return the List of candidate Advisors
*/
protected List<Advisor> findCandidateAdvisors() {
Assert.state(this.advisorRetrievalHelper != null, "No BeanFactoryAdvisorRetrievalHelper available");
return this.advisorRetrievalHelper.findAdvisorBeans();
}
/**
* Search the given candidate Advisors to find all Advisors that
* can apply to the specified bean.View on GitHub (pinned to e8729d0438)
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.
Example fix
// before
public class MyAspect implements Ordered {
public int getOrder() { return depBean.getPriority(); } // triggers creation
}
// after
@Order(5)
public class MyAspect { ... } Defensive patterns
Strategy: validation
Validate before calling
// Detect advisors implementing Ordered whose getOrder may trigger bean creation
for (Advisor a : candidateAdvisors) {
if (a instanceof org.springframework.core.Ordered
&& a.getClass().isAnnotationPresent(org.springframework.core.annotation.Order.class) == false) {
log.warn("Advisor {} implements Ordered; prefer @Order to avoid eager bean creation during sort", a.getClass());
} Type guard
public static boolean usesOrderedInterface(Advisor a) {
return a instanceof org.springframework.core.Ordered;
} Try / catch
try {
return autoProxyCreator.postProcessAfterInitialization(bean, beanName);
} catch (AopConfigException ex) {
if (ex.getMessage().contains("Advisor sorting failed")) {
log.error("Convert Ordered advisors to @Order to break circular bean creation", ex);
}
throw ex;
} Prevention
- Annotate advisors with @Order instead of implementing Ordered.
- Keep any getOrder() implementation free of bean lookups.
- Break circular dependencies surfaced at advisor-sort time.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- AdvisorAutoProxyCreator requires a ConfigurableListableBeanF
- DeclarePrecedence not presently supported in Spring AOP
- Class name [{}] is not a known auto-proxy creator class
- Unable to instantiate proxy using Objenesis, and regular pro
- Cannot create class proxy for TargetSource with null target
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/a3326572b96c0afc.json.
Report an issue: GitHub.