spring-projects/spring-framework · error · AopConfigException

Advisor sorting failed with unexpected bean creation…

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

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.

Solutions

  1. 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.
  2. Ensure getOrder() is a pure constant return with no dependency access.
  3. Break the circular dependency by decoupling the order value from collaborator beans (use a static literal).
  4. Mark the offending dependency @Lazy so it is not eagerly created during getOrder().

Example fix

// before
@Component
public class MyAdvisor implements Ordered {
    @Autowired SomeDependency dep; // forces creation during getOrder()
    public int getOrder() { return dep.priority(); }
}

// after
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
public class MyAdvisor {
    @Autowired @Lazy SomeDependency dep;
}
Defensive patterns

Strategy: validation

Validate before calling

// Order advisors declaratively to avoid bean creation during sorting
@Component
@Order(10)
public class MyAdvisor { /* no Ordered.getOrder() dependency access */ }

Type guard

// Ensure an advisor does not implement Ordered with stateful getOrder()
static boolean safeOrdering(Class<?> advisorType) {
    return !Ordered.class.isAssignableFrom(advisorType);
}

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/a3326572b96c0afc. Report an issue: GitHub.

Appendix: 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 69bf83ad71)