spring-projects/spring-framework · error · IllegalArgumentException
Number of 'constructorArgs' ({}) must match number of 'const
Error message
Number of 'constructorArgs' ({}) must match number of 'constructorArgTypes' ({}) What it means
setConstructorArguments() (line 150) requires the values array and the types array to have the same length, because each arg must be paired with exactly one declared parameter type for constructor resolution. A length mismatch means the inputs cannot describe a single constructor invocation.
Source
Thrown at spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java:151
* exception in this case, rather than let a mysterious failure happen later.
*/
public CglibAopProxy(AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null");
this.advised = config;
this.advisedDispatcher = new AdvisedDispatcher(this.advised);
}
/**
* Set constructor arguments to use for creating the proxy.
* @param constructorArgs the constructor argument values
* @param constructorArgTypes the constructor argument types
*/
public void setConstructorArguments(Object @Nullable [] constructorArgs, Class<?> @Nullable [] constructorArgTypes) {
if (constructorArgs == null || constructorArgTypes == null) {
throw new IllegalArgumentException("Both 'constructorArgs' and 'constructorArgTypes' need to be specified");
}
if (constructorArgs.length != constructorArgTypes.length) {
throw new IllegalArgumentException("Number of 'constructorArgs' (" + constructorArgs.length +
") must match number of 'constructorArgTypes' (" + constructorArgTypes.length + ")");
}
this.constructorArgs = constructorArgs;
this.constructorArgTypes = constructorArgTypes;
}
@Override
public Object getProxy() {
return buildProxy(null, false);
}
@Override
public Object getProxy(@Nullable ClassLoader classLoader) {
return buildProxy(classLoader, false);
}
@OverrideView on GitHub (pinned to 69bf83ad71)
Solutions
- Make the two arrays the same length, pairing every value with its type
- Build them together: for each (value,type) pair push to both arrays in one loop
- Add an assertion: if (args.length != types.length) throw before calling the API
Example fix
// before
proxy.setConstructorArguments(new Object[]{1, "x"}, new Class[]{int.class});
// after
proxy.setConstructorArguments(new Object[]{1, "x"}, new Class[]{int.class, String.class}); Defensive patterns
Strategy: validation
Validate before calling
if (constructorArgs.length != constructorArgTypes.length) {
throw new IllegalArgumentException("constructorArgs/constructorArgTypes length mismatch");
} Type guard
private static boolean constructorArgsConsistent(Object[] args, Class<?>[] types) {
return args != null && types != null && args.length == types.length;
} Prevention
- Build args and types in a single loop so they cannot diverge
- Add an assert/Objects.checkIndex-style guard before calling the API
When it happens
Trigger: Passing constructorArgs and constructorArgTypes of different lengths, e.g. 3 values but 2 types.
Common situations: Appending an argument but forgetting to add its type; copying arrays of unequal size; off-by-one when building the two arrays in a loop.
Related errors
- Both 'constructorArgs' and 'constructorArgTypes' need to be
- An advice method can never be a constructor
- Failed to invoke aspect constructor: {aspectClass.getName()}
- Property 'targetBeanName' is required
- Property 'methodName' is required
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/fde95906c71a2402.
Report an issue: GitHub.