alibaba/spring-cloud-alibaba · critical · IllegalStateException

ApplicationContext not set

Error message

ApplicationContext not set

What it means

Thrown by NacosAnnotationProcessor.getNacosConfigManager() when the ApplicationContext field is still null. The processor implements ApplicationContextAware via setApplicationContext(...), so this field is populated during the normal Spring bean lifecycle; reaching this guard means getNacosConfigManager() was invoked before setApplicationContext ran, i.e., during an earlier lifecycle phase than the processor is ready for.

Source

Thrown at spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/annotation/NacosAnnotationProcessor.java:785

				if (dataId != null && group != null && key != null && defaultValue != null) {
					handleBeanNacosConfigAnnotation(dataId, group, key, true, beanName, bean, defaultValue);
				}
				}
			}

		}

	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	private NacosConfigManager getNacosConfigManager() {
		if (this.nacosConfigManager == null) {
			if (this.applicationContext == null) {
				throw new IllegalStateException("ApplicationContext not set");
			}
			nacosConfigManager = this.applicationContext.getBean(NacosConfigManager.class);

		}
		return nacosConfigManager;
	}

	private static String[] getNullPropertyNames(Object source) {
		final BeanWrapper src = new BeanWrapperImpl(source);
		PropertyDescriptor[] pds = src.getPropertyDescriptors();

		Set<String> nullPropertyNames = new HashSet<>();
		for (PropertyDescriptor pd : pds) {
			String propertyName = pd.getName();
			try {
				Object propertyValue = src.getPropertyValue(propertyName);
				if (propertyValue == null) {
					nullPropertyNames.add(propertyName);

View on GitHub (pinned to 115d590110)

Solutions

  1. Let Spring create and manage NacosAnnotationProcessor so setApplicationContext is invoked before use.
  2. If you must drive it manually, call setApplicationContext(ctx) before any method that resolves NacosConfigManager.
  3. Check for early-eager initialization (@Autowired/constructor use of the processor) that defeats the ApplicationContextAware ordering.

Example fix

// before (manual, misses context wiring)
NacosAnnotationProcessor p = new NacosAnnotationProcessor();
p.process(...); // -> IllegalStateException
// after
NacosAnnotationProcessor p = ctx.getBean(NacosAnnotationProcessor.class);
p.setApplicationContext(ctx); // if driven manually
p.process(...);
Defensive patterns

Strategy: validation

Validate before calling

// If driving the processor manually, ensure the context is set first.
if (processor instanceof ApplicationContextAware) {
    ((ApplicationContextAware) processor).setApplicationContext(ctx);
}
NacosConfigManager mgr = processor.getNacosConfigManager();

Prevention

When it happens

Trigger: A bean-post-processing or initialization callback in NacosAnnotationProcessor that calls getNacosConfigManager() before the ApplicationContext has been injected (e.g., an early BeanFactoryPostProcessor-style hook, or a manually instantiated processor that was never wired by Spring).

Common situations: Custom bootstrap that instantiates NacosAnnotationProcessor outside Spring; a bean-creation-order change in an upgrade that calls config logic before the ApplicationContext is set; using the processor in a context where ApplicationContextAware is not honored.

Related errors


AI-assisted analysis of alibaba/spring-cloud-alibaba@115d590110 (2026-08-14). Data as JSON: /api/errors/33cf25b757eaae53. Report an issue: GitHub.