In ConstructorResolver:
private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {
TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
// ...
for (Map.Entry<Integer, ConstructorArgumentValues.ValueHolder> entry : cargs.getIndexedArgumentValues().entrySet()) {
int index = entry.getKey();
if (index < 0) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Invalid constructor argument index: " + index);
}
if (index > minNrOfArgs) {
minNrOfArgs = index + 1;
}
// ....
}
// ....
return minNrOfArgs;
}
I assume that method resolveConstructorArguments is to resolve contructor arguments in the XML file and return the minimum number of parameters required by contructor 。but if the first parameter is autowired , the second parameter is config by XML file,the method will not work well。
example:
public class FactoryObject {
public DmzService getDmz(String name, int age, Date birthDay, OrderService orderService) {
public DmzService getDmz(OrderService orderService,String name) {
return new DmzService(orderService,name);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="constructor">
<bean id="factoryObject" class="com.dmz.spring.first.instantiation.service.FactoryObject"/>
<bean class="com.dmz.spring.first.instantiation.service.OrderService" id="orderService"/>
<bean id="dmzService" factory-bean="factoryObject" factory-method="getDmz">
<constructor-arg index="1" value="dmz"/>
</bean>
</beans>
the resolveConstructorArguments method will return 1,but correct answer is 2。
I think the problem arises because of this judgment:
if (index > minNrOfArgs) {
minNrOfArgs = index + 1;
}
It might be better to change it to look like this
if (index + 1 > minNrOfArgs) {
minNrOfArgs = index + 1;
}