alibaba/COLA · error · RuntimeException
Component can not be found in Spring Container
Error message
Component ${targetClz} can not be found in Spring Container What it means
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.
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.
Example fix
// before
MyService svc = ApplicationContextHelper.getBean(MyService.class); // throws if not a bean
// after
@Component // ensure class is registered and scanned
public class MyService { ... }
MyService svc = ApplicationContextHelper.getBean(MyService.class); Defensive patterns
Strategy: try-catch
Validate before calling
if (!beanDefined(ApplicationContextHelper.getApplicationContext(), MyService.class)) {
throw new IllegalStateException("MyService is not registered as a Spring bean");
} Type guard
public static <T> boolean isSpringBeanAvailable(ApplicationContext ctx, Class<T> clz) {
if (ctx == null) return false;
String name = Character.toLowerCase(clz.getSimpleName().charAt(0)) + clz.getSimpleName().substring(1);
return ctx.getBeanNamesForType(clz).length > 0 || ctx.containsBean(name);
} Try / catch
try {
MyService svc = ApplicationContextHelper.getBean(MyService.class);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().startsWith("Component ")) {
// fall back: construct manually or log configuration error
} else {
throw e;
}
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
AI-assisted analysis of alibaba/COLA@352e1a8675 (2026-09-08).
Data as JSON: /api/errors/ae5c52a4bd648cdb.
Report an issue: GitHub.
Appendix: source
Thrown at cola-archetypes/cola-archetype-light/src/main/resources/archetype-resources/src/main/java/domain/ApplicationContextHelper.java:35
ApplicationContextHelper.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> targetClz) {
T beanInstance = null;
//优先按type查
try {
beanInstance = (T)applicationContext.getBean(targetClz);
} catch (Exception e) {
}
//按name查
if (beanInstance == null) {
String simpleName = targetClz.getSimpleName();
//首字母小写
simpleName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
beanInstance = (T)applicationContext.getBean(simpleName);
}
if (beanInstance == null) {
throw new RuntimeException("Component " + targetClz + " can not be found in Spring Container");
}
return beanInstance;
}
public static Object getBean(String claz) {
return ApplicationContextHelper.applicationContext.getBean(claz);
}
public static <T> T getBean(String name, Class<T> requiredType) {
return ApplicationContextHelper.applicationContext.getBean(name, requiredType);
}
public static <T> T getBean(Class<T> requiredType, Object... params) {
return ApplicationContextHelper.applicationContext.getBean(requiredType, params);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
View on GitHub (pinned to 352e1a8675)