为什么在尝试使用constructor-arg元素初始化bean时会出现BeanCreationException

Dun*_*nes 3 java spring

我正在尝试创建一个不可变对象并在spring中从xml配置文件初始化它.但是我得到了一个BeanCreationException并且我无法找出原因.该异常指出它找不到具有以下消息的适当构造函数:

"无法解析匹配的构造函数(提示:为简单参数指定索引/类型/名称参数以避免类型歧义)"

但是,如果我将constructor-arg元素更改为使用基于索引的参数解析它可以正常工作,但这不会产生可读的配置文件.也就是说,我想要基于名称的参数解析,以便很容易看出参数对应的内容.

据我所知,根本没有歧义.也就是说,只有一个两个args构造函数.它需要两个整数,一次称为'a',一个称为'b',这正是bean元素指定的内容

所有文件都是UTF-8编码,因此不能成为编码问题.

例外:

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)
Run Code Online (Sandbox Code Playgroud)

conifg提取物:

<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>
Run Code Online (Sandbox Code Playgroud)

类:

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);       
    }

}
Run Code Online (Sandbox Code Playgroud)

dun*_*nni 7

属性"name"的文档说:

注意:这需要将调试符号存储在类文件中,以便内省参数名称!

那么也许您没有启用调试符号?