{"record":{"id":"cd32cf3643d55009","repo":"spring-projects/spring-framework","slug":"injection-of-autowired-dependencies-failed-for-cla","errorCode":null,"errorMessage":"Injection of autowired dependencies failed for class [{}]","messagePattern":"Injection of autowired dependencies failed for class \\[(.+?)\\]","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java","lineNumber":522,"sourceCode":"\t/**\n\t * <em>Native</em> processing method for direct calls with an arbitrary target\n\t * instance, resolving all of its fields and methods which are annotated with\n\t * one of the configured 'autowired' annotation types.\n\t * @param bean the target instance to process\n\t * @throws BeanCreationException if autowiring failed\n\t * @see #setAutowiredAnnotationTypes(Set)\n\t */\n\tpublic void processInjection(Object bean) throws BeanCreationException {\n\t\tClass<?> clazz = bean.getClass();\n\t\tInjectionMetadata metadata = findAutowiringMetadata(clazz.getName(), clazz, null);\n\t\ttry {\n\t\t\tmetadata.inject(bean, null, null);\n\t\t}\n\t\tcatch (BeanCreationException ex) {\n\t\t\tthrow ex;\n\t\t}\n\t\tcatch (Throwable ex) {\n\t\t\tthrow new BeanCreationException(\n\t\t\t\t\t\"Injection of autowired dependencies failed for class [\" + clazz + \"]\", ex);\n\t\t}\n\t}\n\n\tprivate InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {\n\t\t// Fall back to class name as cache key, for backwards compatibility with custom callers.\n\t\tString cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());\n\t\t// Quick check on the concurrent map first, with minimal locking.\n\t\tInjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);\n\t\tif (InjectionMetadata.needsRefresh(metadata, clazz)) {\n\t\t\tsynchronized (this.injectionMetadataCache) {\n\t\t\t\tmetadata = this.injectionMetadataCache.get(cacheKey);\n\t\t\t\tif (InjectionMetadata.needsRefresh(metadata, clazz)) {\n\t\t\t\t\tif (metadata != null) {\n\t\t\t\t\t\tmetadata.clear(pvs);\n\t\t\t\t\t}\n\t\t\t\t\tmetadata = buildAutowiringMetadata(clazz);\n\t\t\t\t\tthis.injectionMetadataCache.put(cacheKey, metadata);","sourceCodeStart":504,"sourceCodeEnd":540,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java#L504-L540","documentation":"Thrown by the public processInjection(Object bean) method — used for manual/direct injection outside the normal container lifecycle — when injection fails with a non-BeanCreationException Throwable. This is the native API counterpart of error 203; it includes the class name in the message because the bean name may be null (the method accepts a raw object).","triggerScenarios":"Calling autowiredAnnotationBeanPostProcessor.processInjection(myObject) on a manually created object where one of its @Autowired fields/methods cannot be resolved (missing bean, type mismatch, or a reflective error).","commonSituations":"Using @Autowired on objects created outside Spring (e.g., in a test harness or legacy integration) via processInjection. Manually wiring a non-Spring-managed object that has missing or ambiguous dependencies.","solutions":["Inspect the nested cause (ex.getCause()) for the real failure — it is often a NoSuchBeanDefinitionException or UnsatisfiedDependencyException.","Ensure all required dependencies are available in the BeanFactory before calling processInjection.","Prefer registering the object as a Spring bean instead of manual processInjection, so the full container lifecycle applies."],"exampleFix":"// before\nMyBean bean = new MyBean();\npostProcessor.processInjection(bean); // fails if deps missing\n\n// after\n// Register as a proper bean so dependencies resolve via container\n@Bean\nMyBean myBean() { return new MyBean(); }","handlingStrategy":"try-catch","validationCode":"// Before manual injection, verify all @Autowired dependencies are resolvable\nClass<?> clazz = bean.getClass();\nfor (Field f : clazz.getDeclaredFields()) {\n    if (f.isAnnotationPresent(Autowired.class)) {\n        Class<?> type = f.getType();\n        if (beanFactory.getBeanNamesForType(type).length == 0\n                && f.getAnnotation(Autowired.class).required()) {\n            // dependency will fail — register it first\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    postProcessor.processInjection(myBean);\n} catch (BeanCreationException ex) {\n    // message: \"Injection of autowired dependencies failed for class [...]\"\n    Throwable cause = ex.getCause();\n    log.error(\"Manual injection failed\", cause);\n}","preventionTips":["Prefer declaring the object as a Spring bean over manual processInjection.","Verify all dependencies exist in the BeanFactory before calling processInjection.","Use processInjection only for edge cases like third-party objects, and always wrap in try-catch."],"tags":["spring","autowiring","injection","manual-wiring"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}