alibaba/spring-cloud-alibaba · error · IllegalStateException
ApplicationContext or FeignClientFactory not initialized
Error message
ApplicationContext or FeignClientFactory not initialized
What it means
SentinelFeign.Builder.internalBuild() wires a custom InvocationHandlerFactory that needs both the Spring ApplicationContext and the FeignClientFactory (child context) to resolve fallback beans. If either was never set on the builder, this IllegalStateException is thrown when the feign client is first built. It indicates the Sentinel feign integration was assembled without its required Spring wiring.
Source
Thrown at spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/feign/SentinelFeign.java:90
throw new UnsupportedOperationException();
}
@Override
public Builder contract(Contract contract) {
this.contract = contract;
return this;
}
@Override
public Feign internalBuild() {
super.invocationHandlerFactory(new InvocationHandlerFactory() {
@Override
public InvocationHandler create(Target target,
Map<Method, MethodHandler> dispatch) {
ApplicationContext ctx = Builder.this.applicationContext;
FeignClientFactory factory = Builder.this.feignClientFactory;
if (ctx == null || factory == null) {
throw new IllegalStateException("ApplicationContext or FeignClientFactory not initialized");
}
GenericApplicationContext gctx = (GenericApplicationContext) ctx;
BeanDefinition def = gctx.getBeanDefinition(target.type().getName());
@Nullable FeignClientFactoryBean feignClientFactoryBean;
// If you need the attributes to be resolved lazily, set the property value to true.
Boolean isLazyInit = ctx.getEnvironment()
.getProperty(FEIGN_LAZY_ATTR_RESOLUTION, Boolean.class, false);
if (isLazyInit) {
/*
* Due to the change of the initialization sequence,
* BeanFactory.getBean will cause a circular dependency. So
* FeignClientFactoryBean can only be obtained from BeanDefinition
*/
feignClientFactoryBean = (FeignClientFactoryBean) def
.getAttribute("feignClientsRegistrarFactoryBean");
}
else {View on GitHub (pinned to 115d590110)
Solutions
- Ensure the sentinel-feign starter and feign auto-configuration are active: keep spring.cloud.sentinel.feign.enabled=true (default).
- Do not construct SentinelFeign builders manually; let SentinelFeignAutoConfiguration build them so context and factory are injected.
- If you must build manually, set applicationContext(...) and feignClientFactory(...) on the builder before build().
- Check that @EnableFeignClients is present and the feign client context is created.
Example fix
// before SentinelFeign.builder().target(MyApi.class, url); // after SentinelFeign.builder().applicationContext(ctx).feignClientFactory(factory).target(MyApi.class, url);
Defensive patterns
Strategy: validation
Validate before calling
void build(SentinelFeign.Builder b) {
if (b == null || b.getApplicationContext() == null || b.getFeignClientFactory() == null)
throw new IllegalStateException("SentinelFeign builder needs both applicationContext and feignClientFactory");
b.build();
} Try / catch
try { client = feignClient(); }
catch (IllegalStateException e) { if (e.getMessage().contains("not initialized")) { /* re-init context */ } throw e; } Prevention
- Let SentinelFeignAutoConfiguration build feign clients.
- Keep spring.cloud.sentinel.feign.enabled=true.
- Do not construct SentinelFeign builders by hand.
When it happens
Trigger: Manually constructing a SentinelFeign builder without injecting applicationContext/feignClientFactory. A misconfigured auto-configuration where SentinelFeignAutoConfiguration did not run (e.g. feign not enabled, starter missing).
Common situations: Excluding the sentinel feign auto-configuration. Disabling spring.cloud.sentinel.feign.enabled. A custom Feign.Builder that bypasses the auto-config path.
Related errors
- ConfigService is not initialized
- {type} class attribute exists but {type} method attribute is
- {type} method attribute exists but {type} class attribute is
- FeignClientFactoryBean not found for {targetType}
- FeignClientFactory not initialized
AI-assisted analysis of alibaba/spring-cloud-alibaba@115d590110 (2026-08-14).
Data as JSON: /api/errors/3b119d25136e9275.
Report an issue: GitHub.