{"record":{"id":"ae5c52a4bd648cdb","repo":"alibaba/COLA","slug":"component-targetclz-can-not-be-found-in-spring","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-archetypes/cola-archetype-light/src/main/resources/archetype-resources/src/main/java/domain/ApplicationContextHelper.java","lineNumber":35,"sourceCode":"        ApplicationContextHelper.applicationContext = applicationContext;\r\n    }\r\n\r\n    public static <T> T getBean(Class<T> targetClz) {\r\n        T beanInstance = null;\r\n        //优先按type查\r\n        try {\r\n            beanInstance = (T)applicationContext.getBean(targetClz);\r\n        } catch (Exception e) {\r\n        }\r\n        //按name查\r\n        if (beanInstance == null) {\r\n            String simpleName = targetClz.getSimpleName();\r\n            //首字母小写\r\n            simpleName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);\r\n            beanInstance = (T)applicationContext.getBean(simpleName);\r\n        }\r\n        if (beanInstance == null) {\r\n            throw new RuntimeException(\"Component \" + targetClz + \" can not be found in Spring Container\");\r\n        }\r\n        return beanInstance;\r\n    }\r\n\r\n    public static Object getBean(String claz) {\r\n        return ApplicationContextHelper.applicationContext.getBean(claz);\r\n    }\r\n\r\n    public static <T> T getBean(String name, Class<T> requiredType) {\r\n        return ApplicationContextHelper.applicationContext.getBean(name, requiredType);\r\n    }\r\n\r\n    public static <T> T getBean(Class<T> requiredType, Object... params) {\r\n        return ApplicationContextHelper.applicationContext.getBean(requiredType, params);\r\n    }\r\n\r\n    public static ApplicationContext getApplicationContext() {\r\n        return applicationContext;\r","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-archetypes/cola-archetype-light/src/main/resources/archetype-resources/src/main/java/domain/ApplicationContextHelper.java#L17-L53","documentation":"ApplicationContextHelper.getBean(Class) is a static convenience for fetching a Spring bean by type. It first tries applicationContext.getBean(targetClz), then falls back to a lookup by a de-capitalized simple class name. If both lookups return nothing, it throws this RuntimeException, meaning no bean of that class (or conventional bean name) is registered in the Spring container.","triggerScenarios":"Calling ApplicationContextHelper.getBean(SomeClass.class) when the class is not annotated as a Spring component (e.g. missing @Component/@Service) or is not covered by component scanning; also when the applicationContext static field has not been injected yet (helper used before the Spring context finished refreshing).","commonSituations":"A newly created class was forgotten in a package excluded from @ComponentScan; using the helper in a unit test without a Spring context; using ApplicationContextHelper in code that runs during context startup before setApplicationContext is called; the bean is registered under a custom name that doesn't match the de-capitalized simple name.","solutions":["Verify the class is annotated with a Spring stereotype (@Component/@Service/@Repository) and its package is included in component scanning.","Ensure the bean name matches the lookup: if the bean has a custom @Component(\"name\"), look it up by that name via getBean(String).","Check that ApplicationContextHelper.applicationContext is populated (class implements ApplicationContextAware and is itself registered as a bean) before calling getBean.","If used in tests, bootstrap a Spring context (@SpringBootTest or ApplicationContextRunner) instead of calling the helper statically."],"exampleFix":"// before\nMyService svc = ApplicationContextHelper.getBean(MyService.class); // throws if not a bean\n// after\n@Component // ensure class is registered and scanned\npublic class MyService { ... }\nMyService svc = ApplicationContextHelper.getBean(MyService.class);","handlingStrategy":"try-catch","validationCode":"if (!beanDefined(ApplicationContextHelper.getApplicationContext(), MyService.class)) {\n    throw new IllegalStateException(\"MyService is not registered as a Spring bean\");\n}","typeGuard":"public static <T> boolean isSpringBeanAvailable(ApplicationContext ctx, Class<T> clz) {\n    if (ctx == null) return false;\n    String name = Character.toLowerCase(clz.getSimpleName().charAt(0)) + clz.getSimpleName().substring(1);\n    return ctx.getBeanNamesForType(clz).length > 0 || ctx.containsBean(name);\n}","tryCatchPattern":"try {\n    MyService svc = ApplicationContextHelper.getBean(MyService.class);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Component \")) {\n        // fall back: construct manually or log configuration error\n    } else {\n        throw e;\n    }\n}","preventionTips":["Always annotate implementation classes with a Spring stereotype and verify the package is within @ComponentScan.","Never call ApplicationContextHelper before the Spring context is fully refreshed.","Prefer constructor injection over static bean lookup; reserve the helper for framework extension points.","Add a startup check/test that resolves all statically-looked-up beans once the context is ready."],"tags":["spring","dependency-injection","bean-lookup"],"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"}