{"record":{"id":"fd73d5e08d370d34","repo":"spring-projects/spring-framework","slug":"invalid-autowire-marked-constructor-found-con","errorCode":null,"errorMessage":"Invalid autowire-marked constructor: {}. Found constructor with 'required' Autowired annotation already: {}","messagePattern":"Invalid autowire-marked constructor: (.+?)\\. Found constructor with 'required' Autowired annotation already: (.+?)","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java","lineNumber":398,"sourceCode":"\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);\n\t\t\t\t\t\t\tif (userClass != beanClass) {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tConstructor<?> superCtor =\n\t\t\t\t\t\t\t\t\t\t\tuserClass.getDeclaredConstructor(candidate.getParameterTypes());\n\t\t\t\t\t\t\t\t\tann = findAutowiredAnnotation(superCtor);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcatch (NoSuchMethodException ex) {\n\t\t\t\t\t\t\t\t\t// Simply proceed, no equivalent superclass constructor found...\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (ann != null) {\n\t\t\t\t\t\t\tif (requiredConstructor != null) {\n\t\t\t\t\t\t\t\tthrow new BeanCreationException(beanName,\n\t\t\t\t\t\t\t\t\t\t\"Invalid autowire-marked constructor: \" + candidate +\n\t\t\t\t\t\t\t\t\t\t\". Found constructor with 'required' Autowired annotation already: \" +\n\t\t\t\t\t\t\t\t\t\trequiredConstructor);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tboolean required = determineRequiredStatus(ann);\n\t\t\t\t\t\t\tif (required) {\n\t\t\t\t\t\t\t\tif (!candidates.isEmpty()) {\n\t\t\t\t\t\t\t\t\tthrow new BeanCreationException(beanName,\n\t\t\t\t\t\t\t\t\t\t\t\"Invalid autowire-marked constructors: \" + candidates +\n\t\t\t\t\t\t\t\t\t\t\t\". Found constructor with 'required' Autowired annotation: \" +\n\t\t\t\t\t\t\t\t\t\t\tcandidate);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\trequiredConstructor = candidate;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcandidates.add(candidate);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (candidate.getParameterCount() == 0) {\n\t\t\t\t\t\t\tdefaultConstructor = candidate;","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java#L380-L416","documentation":"BeanCreationException thrown by determineCandidateConstructors when more than one constructor is annotated with @Autowired and at least two are 'required' (the default). Spring allows exactly one required autowire constructor; a second required one is ambiguous and rejected at line 398. Optional (required=false) autowired constructors are tolerated alongside a required one.","triggerScenarios":"A class declares @Autowired on two constructors both defaulting to required=true (or explicitly required=true). During candidate-constructor scanning, once requiredConstructor is set, a second required annotation trips the check.","commonSituations":"Copy-pasting @Autowired onto a newly added constructor; merging two classes each with its own @Autowired constructor; misunderstanding that only one constructor may be the autowire target.","solutions":["Leave @Autowired on exactly one constructor (the canonical one) and remove it from the others.","If multiple constructors are intentional, mark all but one @Autowired(required = false).","Use a single constructor and omit @Autowired entirely (Spring 4.3+ autowires a single constructor implicitly)."],"exampleFix":"// before\n@Autowired\npublic Service(Repo r) { ... }\n@Autowired\npublic Service(Repo r, Cache c) { ... } // BeanCreationException\n\n// after\n@Autowired\npublic Service(Repo r, Cache c) { ... }\npublic Service(Repo r) { this(r, null); }","handlingStrategy":"validation","validationCode":"// Fail fast at startup: at most one @Autowired(required=true) constructor\nlong requiredCount = Arrays.stream(beanClass.getDeclaredConstructors())\n    .filter(c -> AnnotationUtils.findAnnotation(c, Autowired.class) != null)\n    .filter(c -> c.getAnnotation(Autowired.class).required())\n    .count();\nif (requiredCount > 1) throw new IllegalStateException(\"multiple required constructors\");","typeGuard":"static boolean hasSingleRequiredAutowired(Class<?> c) {\n    long n = Arrays.stream(c.getDeclaredConstructors())\n        .filter(m -> Optional.ofNullable(m.getAnnotation(Autowired.class))\n            .map(Autowired::required).orElse(false)).count();\n    return n <= 1;\n}","tryCatchPattern":null,"preventionTips":["Keep a single canonical constructor and omit @Autowired (Spring autowires it implicitly).","If multiple constructors are needed, mark all but one @Autowired(required = false).","Add a component test that loads the context to surface duplicate-annotation errors early."],"tags":["dependency-injection","autowired","constructor-injection","bean-creation"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}