{"record":{"id":"dbd6ae510216b7cf","repo":"alibaba/COLA","slug":"component-targetclz-can-not-be-found-in-spring-dbd6ae","errorCode":null,"errorMessage":"Component ${targetClz} can not be found in Spring Container","messagePattern":"Component (.+?) can not be found in Spring Container","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"cola-components/cola-component-domain-starter/src/main/java/com/alibaba/cola/domain/ApplicationContextHelper.java","lineNumber":38,"sourceCode":"        ApplicationContextHelper.applicationContext = applicationContext;\n    }\n\n    public static <T> T getBean(Class<T> targetClz) {\n        T beanInstance = null;\n        //优先按type查\n        try {\n            beanInstance = (T)applicationContext.getBean(targetClz);\n        } catch (Exception e) {\n        }\n        //按name查\n        if (beanInstance == null) {\n            String simpleName = targetClz.getSimpleName();\n            //首字母小写\n            simpleName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);\n            beanInstance = (T)applicationContext.getBean(simpleName);\n        }\n        if (beanInstance == null) {\n            throw new RuntimeException(\"Component \" + targetClz + \" can not be found in Spring Container\");\n        }\n        return beanInstance;\n    }\n\n    public static Object getBean(String claz) {\n        return ApplicationContextHelper.applicationContext.getBean(claz);\n    }\n\n    public static <T> T getBean(String name, Class<T> requiredType) {\n        return ApplicationContextHelper.applicationContext.getBean(name, requiredType);\n    }\n\n    public static <T> T getBean(Class<T> requiredType, Object... params) {\n        return ApplicationContextHelper.applicationContext.getBean(requiredType, params);\n    }\n\n    public static ApplicationContext getApplicationContext() {\n        return applicationContext;","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-components/cola-component-domain-starter/src/main/java/com/alibaba/cola/domain/ApplicationContextHelper.java#L20-L56","documentation":"The cola-component-domain-starter ApplicationContextHelper provides static access to Spring beans. getBean(Class) tries a by-type lookup then a de-capitalized simple-name lookup; if neither resolves a bean, it throws this RuntimeException, indicating the requested component is absent from the Spring context.","triggerScenarios":"Calling ApplicationContextHelper.getBean(SomeGateway.class) when the implementation class isn't a registered Spring bean (missing annotation or outside component scan), or when applicationContext hasn't been injected (helper used outside a running Spring context).","commonSituations":"Domain code fetching a Gateway/Repository implementation that was never annotated @Repository; using the helper in non-Spring unit tests; bean defined with a custom name mismatching the conventional lookup; premature use during context startup.","solutions":["Annotate the implementation class (@Component/@Service/@Repository) and ensure its package is component-scanned.","Confirm the registered bean name equals the de-capitalized simple class name, or look up by explicit name via getBean(String).","Verify the Spring context is fully started and ApplicationContextHelper has received the ApplicationContext (ApplicationContextAware wiring).","In tests, use a Spring test context rather than calling the static helper bare."],"exampleFix":"// before\nOrderGateway gw = ApplicationContextHelper.getBean(OrderGatewayImpl.class); // throws\n// after\n@Repository // register the implementation\npublic class OrderGatewayImpl implements OrderGateway { ... }\nOrderGateway gw = ApplicationContextHelper.getBean(OrderGateway.class);","handlingStrategy":"try-catch","validationCode":"ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();\nif (ctx == null || ctx.getBeanNamesForType(OrderGateway.class).length == 0) {\n    throw new IllegalStateException(\"OrderGateway is not registered in the Spring container\");\n}","typeGuard":"public static <T> boolean beanExists(ApplicationContext ctx, Class<T> clz) {\n    return ctx != null && ctx.getBeanNamesForType(clz).length > 0;\n}","tryCatchPattern":"try {\n    OrderGateway gw = ApplicationContextHelper.getBean(OrderGateway.class);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"can not be found in Spring Container\")) {\n        // register/fall back to a default implementation\n    } else {\n        throw e;\n    }\n}","preventionTips":["Ensure domain interfaces (Gateways/Repositories) have @Repository/@Component annotated implementations inside scanned packages.","Add a context-started integration test that resolves every statically fetched bean.","Avoid static lookup during early startup; use the helper only after the context is fully initialized.","Prefer constructor/setter injection where possible instead of ApplicationContextHelper.getBean."],"tags":["spring","dependency-injection","bean-lookup","cola"],"backgroundTag":"resource-not-found","analyzedSha":"352e1a867538d9cd40e1b11e4028fa652fc97557","analyzedAt":"2026-09-08T00:46:23.972Z","contentChangedAt":"2026-09-08T00:46:23.972Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}