{"record":{"id":"dd687335a60827eb","repo":"spring-projects/spring-framework","slug":"invalid-autowire-marked-constructors-found-co","errorCode":null,"errorMessage":"Invalid autowire-marked constructors: {}. Found constructor with 'required' Autowired annotation: {}","messagePattern":"Invalid autowire-marked constructors: (.+?)\\. Found constructor with 'required' Autowired annotation: (.+?)","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java","lineNumber":406,"sourceCode":"\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;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (!candidates.isEmpty()) {\n\t\t\t\t\t\t// Add default constructor to list of optional constructors, as fallback.\n\t\t\t\t\t\tif (requiredConstructor == null) {\n\t\t\t\t\t\t\tif (defaultConstructor != null) {\n\t\t\t\t\t\t\t\tcandidates.add(defaultConstructor);\n\t\t\t\t\t\t\t}","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java#L388-L424","documentation":"Thrown by determineCandidateConstructors() when a class declares multiple @Autowired constructors and at least one of them is marked required=true (the default). Spring permits multiple @Autowired constructors only if all of them are optional (required=false); at most one constructor may be required. When a second required constructor or any preceding optional candidate already exists and a required one is then encountered, the combination is ambiguous and Spring refuses to pick an injection constructor.","triggerScenarios":"A class has two or more constructors annotated with @Autowired where at least one uses the default (required=true). For example: @Autowired public Foo(DepA a) {} and @Autowired public Foo(DepB b) {} — both default to required, triggering the check at line 405-406 where candidates is non-empty and a required constructor is being added.","commonSituations":"Adding a second constructor with @Autowired during refactoring without realizing the first already carries required=true (the default). Using Lombok @AllArgsConstructor/@RequiredArgsConstructor alongside a hand-written @Autowired constructor. Migrating from field injection to constructor injection and accidentally annotating multiple constructors.","solutions":["Mark all but one constructor with @Autowired(required=false), leaving exactly zero or one required constructor.","Remove @Autowired from extra constructors entirely — Spring can auto-detect a single constructor without the annotation.","Consolidate to a single constructor that accepts all dependencies."],"exampleFix":"// before\n@Autowired\npublic Foo(DepA a) { ... }\n\n@Autowired\npublic Foo(DepB b) { ... }\n\n// after\n@Autowired\npublic Foo(DepA a, DepB b) { ... }","handlingStrategy":"validation","validationCode":"// At startup or in a test, verify only one required @Autowired constructor exists\nConstructor<?>[] ctors = MyBean.class.getDeclaredConstructors();\nlong requiredCount = Arrays.stream(ctors)\n    .filter(c -> c.isAnnotationPresent(Autowired.class))\n    .filter(c -> {\n        Autowired a = c.getAnnotation(Autowired.class);\n        return a.required();\n    })\n    .count();\nif (requiredCount > 1) {\n    throw new IllegalStateException(\"Multiple required @Autowired constructors on \" + MyBean.class);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use at most one @Autowired constructor per class; rely on Spring's single-constructor auto-detection.","Prefer constructor injection with a single constructor and omit @Autowired entirely.","Add a unit test that uses reflection to assert no more than one required @Autowired constructor exists."],"tags":["spring","autowiring","constructor-injection","di"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}