Quick revision
The short answer
Matching a constructor parameter name to a bean name only works if Spring can read the parameter names at runtime. Before 6.1, Spring could fall back to LocalVariableTableParameterNameDiscoverer, which read them from debug information in the class files; Spring Framework 6.1 removed it. Now the names are only available when the code is compiled with the -parameters flag, which Spring Boot's build plugins set but custom builds may not. The robust fix is an explicit @Qualifier.
What to remember
In this answer, we saw that matching constructor parameters to bean names needs the parameter names at runtime. Spring used to read them from debug information with LocalVariableTableParameterNameDiscoverer, but Spring Framework 6.1 removed it, so names now come only from the MethodParameters attribute that javac writes with -parameters. Spring Boot's build plugins set that flag, while custom builds may not, which turns a previously resolved ambiguity into NoUniqueBeanDefinitionException. Adding -parameters fixes the build, and an explicit @Qualifier makes the injection independent of names altogether.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Matching a constructor parameter name to a bean name only works if Spring can read the parameter names at runtime. Before 6.1, Spring could fall back to LocalVariableTableParameterNameDiscoverer, which read them from debug information in the class files; Spring Framework 6.1 removed it. Now the names are only available when the code is compiled with the -parameters flag, which Spring Boot's build plugins set but custom builds may not. The robust fix is an explicit @Qualifier.
Why this question is asked
Upgrades that change nothing in our code and still break startup are the hardest to diagnose, because the code that fails looks exactly as it did yesterday. This one is a classic from the Spring Framework 6.1 upgrade. Interviewers ask it because it tests whether we know where Spring gets information that Java does not keep by default, and because the lesson, do not rely on implicit behaviour for something important, applies far beyond this case. In this answer, we will recap how name matching resolves ambiguity, reproduce the failure, see why 6.1 changed it, fix it in the build and in the code, and compare the two fixes.
1A Quick Recap: Names as a Tie-Breaker
When a constructor parameter has a type with several matching beans, Spring tries a few tie-breakers. If no bean is @Primary and none has a winning @Priority, it compares the parameter's name with the bean names. A parameter called reportingDataSource gets the bean named reportingDataSource. Many code bases relied on this without knowing it, because it simply worked.
There is a catch hidden in that rule. Java compiles method and constructor parameter names away by default. The class file keeps the types, but the names arg0 and arg1 are all that reflection sees, unless the compiler was told to keep them.
2Reproducing the Failure
Here is a service with two DataSource beans and no qualifier.
Notice that the code did not change, and neither did the beans. Only Spring's ability to see the word reportingDataSource changed.
3Why Spring 6.1 Changed It
There are two places a parameter name can come from. The official one is the MethodParameters attribute that javac writes when we compile with -parameters; Java reflection exposes it through Parameter.getName(). The unofficial one is the local variable table, part of the debug information javac writes by default with -g, meant for debuggers. For years Spring had a class, LocalVariableTableParameterNameDiscoverer, that parsed the bytecode to read names from that table when the official attribute was missing.
Reading debug information was always a workaround. It depended on how the code was compiled, required bytecode parsing, did not work for interfaces and abstract methods, and fitted poorly with AOT and native images. Spring deprecated it in 6.0 and removed it in 6.1. From then on, Spring reads parameter names only through standard reflection, so without -parameters there is no name to match, the tie-breaker silently stops working, and the ambiguity becomes an error.
| Source | Needs | Spring 6.1+ |
|---|---|---|
MethodParameters attribute | javac -parameters | used, via standard reflection |
| Local variable debug table | javac -g (the default) | no longer read: discoverer removed |
| Nothing | neither flag | names are arg0, arg1: no match |
4Fix 1: Compile With -parameters
If the project uses spring-boot-starter-parent in Maven or the Spring Boot Gradle plugin, -parameters is already configured, which is why many teams never saw this problem. Projects with their own parent POM, a custom Gradle convention plugin, or a library built separately from the application often do not have it.
The javap check is worth knowing: it shows directly whether the names are in the class file. The flag matters for more than injection. @PathVariable and @RequestParam without explicit names, @Value on parameters in some cases, and Spring Data query methods with named parameters all use the same mechanism, so they can fail after the upgrade in the same way.
5Fix 2: Say It Explicitly With @Qualifier
The more robust fix does not depend on the build at all: we name the bean we want.
Now renaming the parameter cannot change which bean we get, and the intent is visible to every reader. Even better, a custom qualifier annotation such as @Reporting removes the string entirely. I would do both: add -parameters to the build, because Spring MVC and other features expect it, and replace name-based injection of important beans with qualifiers, because a silent dependency on a parameter name is exactly the kind of behaviour that breaks on the next upgrade.
6Follow-up questions
How would you find every place that relies on name matching before upgrading?
Search for constructor or @Bean method parameters whose type has more than one bean, which we can list from the Actuator beans endpoint or by calling getBeanNamesForType on each injected type in a test. A simpler practical check is to build without -parameters on Spring 6.0 with the deprecation warnings enabled; Spring 6.0 logs a warning each time it falls back to the debug information, naming the class.
Why did it work in the IDE but not in the CI build?
IDEs often compile with their own settings, and depending on how a project was imported they may or may not pass -parameters, or they may delegate to the build tool only for some tasks. When the IDE and the command-line build disagree, the class files differ, and the behaviour does too. The command-line build is the one that matters, so the flag must be in the build file.
Does this affect Kotlin code?
Kotlin has its own compiler option, -java-parameters, that writes the same MethodParameters attribute, and Spring also has a Kotlin-specific discoverer that reads parameter names from Kotlin metadata when kotlin-reflect is on the classpath. So Kotlin code is usually fine, but mixed Java and Kotlin projects should check that the Java side is compiled with -parameters too.
7Conclusion
In this answer, we saw that matching constructor parameters to bean names needs the parameter names at runtime. Spring used to read them from debug information with LocalVariableTableParameterNameDiscoverer, but Spring Framework 6.1 removed it, so names now come only from the MethodParameters attribute that javac writes with -parameters. Spring Boot's build plugins set that flag, while custom builds may not, which turns a previously resolved ambiguity into NoUniqueBeanDefinitionException. Adding -parameters fixes the build, and an explicit @Qualifier makes the injection independent of names altogether.
Open with
The constructor relied on name matching: two DataSource beans, no primary, and a parameter called reportingDataSource that Spring matched to the bean of the same name. That only works if Spring can see the parameter name at runtime, and Java drops parameter names unless we compile with -parameters, which writes the MethodParameters attribute.
Then, if they want more
Before 6.1, Spring had a fallback, LocalVariableTableParameterNameDiscoverer, that parsed the bytecode and read names from the debug information javac writes by default. It was deprecated in 6.0 and removed in 6.1, because it was a fragile workaround that did not fit AOT.
So after the upgrade, a build without -parameters gives Spring names like arg0, the tie-breaker stops working, and the ambiguity surfaces as NoUniqueBeanDefinitionException. Projects using spring-boot-starter-parent or the Boot Gradle plugin have the flag already, but a custom parent POM or a separately built library often does not; javap -v on the class shows whether MethodParameters is there.
My fix has two parts. I add -parameters to the compiler configuration, because Spring MVC path variables and request parameters without explicit names use the same mechanism. And for important beans I replace name matching with an explicit @Qualifier, or a custom qualifier annotation, so a rename or a build change can never silently switch which DataSource we get.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After upgrading to Spring Framework 6.1, constructor injection that relied on matching...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/057efec0e2/