{"record":{"id":"bf0244f7fce39727","repo":"spring-projects/spring-framework","slug":"resolution-of-declared-constructors-on-bean-class","errorCode":null,"errorMessage":"Resolution of declared constructors on bean Class [{}] from ClassLoader [{}] failed","messagePattern":"Resolution of declared constructors on bean Class \\[(.+?)\\] from ClassLoader \\[(.+?)\\] failed","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java","lineNumber":366,"sourceCode":"\t@Override\n\tpublic Constructor<?> @Nullable [] determineCandidateConstructors(Class<?> beanClass, final String beanName)\n\t\t\tthrows BeanCreationException {\n\n\t\tcheckLookupMethods(beanClass, beanName);\n\n\t\t// Quick check on the concurrent map first, with minimal locking.\n\t\tConstructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);\n\t\tif (candidateConstructors == null) {\n\t\t\t// Fully synchronized resolution now...\n\t\t\tsynchronized (this.candidateConstructorsCache) {\n\t\t\t\tcandidateConstructors = this.candidateConstructorsCache.get(beanClass);\n\t\t\t\tif (candidateConstructors == null) {\n\t\t\t\t\tConstructor<?>[] rawCandidates;\n\t\t\t\t\ttry {\n\t\t\t\t\t\trawCandidates = beanClass.getDeclaredConstructors();\n\t\t\t\t\t}\n\t\t\t\t\tcatch (Throwable ex) {\n\t\t\t\t\t\tthrow new BeanCreationException(beanName,\n\t\t\t\t\t\t\t\t\"Resolution of declared constructors on bean Class [\" + beanClass.getName() +\n\t\t\t\t\t\t\t\t\"] from ClassLoader [\" + beanClass.getClassLoader() + \"] failed\", ex);\n\t\t\t\t\t}\n\t\t\t\t\tList<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);\n\t\t\t\t\tConstructor<?> requiredConstructor = null;\n\t\t\t\t\tConstructor<?> defaultConstructor = null;\n\t\t\t\t\tConstructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);\n\t\t\t\t\tint nonSyntheticConstructors = 0;\n\t\t\t\t\tfor (Constructor<?> candidate : rawCandidates) {\n\t\t\t\t\t\tif (!candidate.isSynthetic()) {\n\t\t\t\t\t\t\tnonSyntheticConstructors++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (primaryConstructor != null) {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tMergedAnnotation<?> ann = findAutowiredAnnotation(candidate);\n\t\t\t\t\t\tif (ann == null) {\n\t\t\t\t\t\t\tClass<?> userClass = ClassUtils.getUserClass(beanClass);","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java#L348-L384","documentation":"BeanCreationException thrown by determineCandidateConstructors when Class.getDeclaredConstructors() throws (any Throwable). This wraps reflection failures — typically classloader/scoping problems, security manager denials, or a malformed/garbage class — so constructor-based autowiring cannot proceed for the named bean class and its classloader.","triggerScenarios":"getDeclaredConstructors() fails on the bean class during @Autowired constructor discovery: class verified but not reflectable, a sealed/hidden class, a stale hot-swapped class, SecurityManager denying reflection, or a corrupted class file in a custom classloader.","commonSituations":"Devtools/restart classloader mismatches; custom classloaders returning broken Class objects; agents/bytecode tools weaving incompatible constructors; restricted (sealed) classes in newer JDKs.","solutions":["Inspect the root cause (the wrapped Throwable) — most often a NoClassDefFoundError/LinkageError naming the missing type.","Ensure the bean class and all its constructor parameter types are loadable by the same classloader.","Disable/remove bytecode agents or devtools restart if they corrupt the class.","Add the reflective access (class.getDeclaredConstructors) to the permitted set if running under a SecurityManager or strong encapsulation."],"exampleFix":"// before — bean constructor references a type not visible to its classloader\npublic OrderProcessor(PaymentGateway gw) { ... }\n\n// after — ensure PaymentGateway is on the same classpath / module exports it\n// (fix module-info or dependency, then restart)\nmodule app {\n    requires payments.api;\n    opens com.example to spring.core;\n}","handlingStrategy":"try-catch","validationCode":"// Probe reflectability before bean creation\ntry {\n    beanClass.getDeclaredConstructors();\n} catch (Throwable t) {\n    throw new IllegalStateException(\"class not reflectable: \" + beanClass, t);\n}","typeGuard":"static boolean isReflectable(Class<?> c) {\n    try { c.getDeclaredConstructors(); return true; } catch (Throwable t) { return false; }\n}","tryCatchPattern":"try {\n    beanClass.getDeclaredConstructors();\n} catch (LinkageError | SecurityException ex) {\n    // fix classloader/encapsulation, then rethrow with context\n    throw new BeanCreationException(beanName, \"constructor resolution failed\", ex);\n}","preventionTips":["Keep the bean class and all constructor parameter types on the same classloader/module path.","Avoid devtools restart / agent weaving that can corrupt class objects.","Open packages to spring.core in module-info under JPMS."],"tags":["dependency-injection","classloader","reflection","bean-creation"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}