Why do I get a BeanCreationException when I try to initialize a bean using the constructor-arg element

I am trying to create an immutable object and initialize it from the xml configuration file in spring. But I get a BeanCreationException, and I couldn't figure out why. An exception indicates that it cannot find the appropriate constructor with the following message:

"Failed to resolve the match constructor (tooltip: provide index / type / name arguments for simple parameters to avoid type ambiguity)

However, if I modify the elements of the argument constructor to use index-based argument resolution, it works fine, but it will not do for a well-read configuration file. That is, I want name-based argument resolution to make it easy to see what matches the argument.

As far as I can see, there is no ambiguity at all. That is, there is only one args constructor. It accepts two ints, once called "a", and one - "b", which exactly indicates the bean element

All files are encoded in UTF-8 encoding, so there cannot be a problem with the encoding.

an exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'constructorTest' defined in class path resource [ApplicationContext.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at com.alertme.energysmart.service.TestClass.main(TestClass.java:50)

conifg extract:

<bean id="constructorTest" class="testpackage.TestClass">  
    <constructor-arg  name="a" value="0" type="int" />
    <constructor-arg  name="b" value="1" type="int" />
</bean>

<bean id="propertyTest" class="testpackage.TestClass">  
    <property name="a" value="0" />
    <property name="b" value="1" />
</bean>

Grade:

package testpackage;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.alertme.lang.IgnoreNullsShortPrefixStyle;

public class TestClass {

    private int a;
    private int b;

    public TestClass() {
        // java bean
    }

    /**
     * This is the targeted constructor
     */
    public  TestClass(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getB() {
        return b;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, IgnoreNullsShortPrefixStyle.get());
    }

    public static void main(String[] args) {
        final BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("ApplicationContext.xml"));
        final TestClass bean = (TestClass) beanFactory.getBean("constructorTest");
        System.out.println(bean);       
    }

}
+3
source share
2 answers

The docs for the "name" attribute indicate:

. .

, ?

+7

spring: spring 3.0 : , , , , spring . ( ), @ConstructorProperties JDK, .

+1

All Articles