spring constructor-arg name UnsatisfiedDependencyException 本来打算发在stack overflow上的,死活发不出去,先记着吧

When I use Spring framework, I encountered a problem:


The stack trace like this:

Dec 10, 2015 9:22:57 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    Info: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17f052a3: startup date [Thu Dec 10 21:22:57 CST 2015]; root of context hierarchy
    Dec 10, 2015 9:22:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    Info: Loading XML bean definitions from class path resource [beans.xml]
    Dec 10, 2015 9:22:57 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
    Warn: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'i' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
    Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'i' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
    Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'i' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
    Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'i' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:735)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.example.Test.main(Test.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
    Related cause:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'i' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:735)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.example.Test.main(Test.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
    
    Process finished with exit code 1




and my code is:


 
package com.example;


    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;


    public class Test {


        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Integer i = context.getBean("i", Integer.class);
            System.out.println("i = " + i);
        }
    }




finally, my Spring configuration metadata is:


<?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">
        <bean id="i" class="java.lang.Integer">
             <constructor-arg name="value" value="12"/>
        </bean>
    </beans>




There is no problem when I use "index" or "type" attribute of the "constructor-arg" element to initialize beans but "name"......I have read the spring framework reference document and cannot find out why...


When i create a class with some overloaded constructors, then initialize this class with "name" attribute will not cause previous problem but I will cause this exception when i try to initialize java.lang.String 
with "name" attribute.And the spring framework reference said "Keep in mind that to make this work out of the box your code must be compiled with the debug flag enabled so that Spring can look up the parameter 
name from the constructor. If you can’t compile your code with debug flag (or don’t want to) you can use @ConstructorProperties JDK annotation to explicitly name your constructor arguments."


Is my configuration metadata's format is wrong? How can I fix this problem?


ps:i use IDEA to develop, and the IDE's javac options "Generate debugging info" is enabled.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值