{"record":{"id":"3b119d25136e9275","repo":"alibaba/spring-cloud-alibaba","slug":"applicationcontext-or-feignclientfactory-not-initi","errorCode":null,"errorMessage":"ApplicationContext or FeignClientFactory not initialized","messagePattern":"ApplicationContext or FeignClientFactory not initialized","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/feign/SentinelFeign.java","lineNumber":90,"sourceCode":"\t\t\tthrow new UnsupportedOperationException();\n\t\t}\n\n\t\t@Override\n\t\tpublic Builder contract(Contract contract) {\n\t\t\tthis.contract = contract;\n\t\t\treturn this;\n\t\t}\n\n\t\t@Override\n\t\tpublic Feign internalBuild() {\n\t\t\tsuper.invocationHandlerFactory(new InvocationHandlerFactory() {\n\t\t\t\t@Override\n\t\t\t\tpublic InvocationHandler create(Target target,\n\t\t\t\t\t\tMap<Method, MethodHandler> dispatch) {\n\t\t\t\t\tApplicationContext ctx = Builder.this.applicationContext;\n\t\t\t\t\tFeignClientFactory factory = Builder.this.feignClientFactory;\n\t\t\t\t\tif (ctx == null || factory == null) {\n\t\t\t\t\t\tthrow new IllegalStateException(\"ApplicationContext or FeignClientFactory not initialized\");\n\t\t\t\t\t}\n\t\t\t\t\tGenericApplicationContext gctx = (GenericApplicationContext) ctx;\n\t\t\t\t\tBeanDefinition def = gctx.getBeanDefinition(target.type().getName());\n\t\t\t\t\t@Nullable FeignClientFactoryBean feignClientFactoryBean;\n\n\t\t\t\t\t// If you need the attributes to be resolved lazily, set the property value to true.\n\t\t\t\t\tBoolean isLazyInit = ctx.getEnvironment()\n\t\t\t\t\t\t\t.getProperty(FEIGN_LAZY_ATTR_RESOLUTION, Boolean.class, false);\n\t\t\t\t\tif (isLazyInit) {\n\t\t\t\t\t\t/*\n\t\t\t\t\t\t * Due to the change of the initialization sequence,\n\t\t\t\t\t\t * BeanFactory.getBean will cause a circular dependency. So\n\t\t\t\t\t\t * FeignClientFactoryBean can only be obtained from BeanDefinition\n\t\t\t\t\t\t */\n\t\t\t\t\t\tfeignClientFactoryBean = (FeignClientFactoryBean) def\n\t\t\t\t\t\t\t\t.getAttribute(\"feignClientsRegistrarFactoryBean\");\n\t\t\t\t\t}\n\t\t\t\t\telse {","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/alibaba/spring-cloud-alibaba/blob/115d5901102009492e05d5ec18c3f79cad4077d0/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/feign/SentinelFeign.java#L72-L108","documentation":"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.","triggerScenarios":"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).","commonSituations":"Excluding the sentinel feign auto-configuration. Disabling spring.cloud.sentinel.feign.enabled. A custom Feign.Builder that bypasses the auto-config path.","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."],"exampleFix":"// before\nSentinelFeign.builder().target(MyApi.class, url);\n// after\nSentinelFeign.builder().applicationContext(ctx).feignClientFactory(factory).target(MyApi.class, url);","handlingStrategy":"validation","validationCode":"void build(SentinelFeign.Builder b) {\n  if (b == null || b.getApplicationContext() == null || b.getFeignClientFactory() == null)\n    throw new IllegalStateException(\"SentinelFeign builder needs both applicationContext and feignClientFactory\");\n  b.build();\n}","typeGuard":null,"tryCatchPattern":"try { client = feignClient(); }\ncatch (IllegalStateException e) { if (e.getMessage().contains(\"not initialized\")) { /* re-init context */ } throw e; }","preventionTips":["Let SentinelFeignAutoConfiguration build feign clients.","Keep spring.cloud.sentinel.feign.enabled=true.","Do not construct SentinelFeign builders by hand."],"tags":["sentinel","feign","startup","config"],"backgroundTag":null,"analyzedSha":"115d5901102009492e05d5ec18c3f79cad4077d0","analyzedAt":"2026-08-14T04:47:13.900Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}