spring constructor-arg name UnsatisfiedDependencyException 本来打算发在stack overflow上的,死活发不出去,先记着吧
来源:互联网 发布:搜狗输入法云计算卸载 编辑:程序博客网 时间:2024/04/27 21:39
When I use Spring framework, I encountered a problem:
and my code is:
finally, my Spring configuration metadata is:
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.
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
- spring constructor-arg name UnsatisfiedDependencyException 本来打算发在stack overflow上的,死活发不出去,先记着吧
- phpmailer在linux下邮件发不出去的问题——给自己加精!!!
- 解决阿里云postfix邮件发不出去的问题
- 解决阿里云postfix邮件发不出去的问题
- spring - constructor-arg 的使用
- spring - constructor-arg 的使用
- ???这是怎么回事发不出去了?
- 如何解决海外邮件发不出去
- 海外邮件发不出去怎么办
- 撒旦撒打算的发
- spring 的applicationContext.xml中的constructor-arg
- spring - property和constructor-arg的使用
- spring - property和constructor-arg的使用
- spring - constructor-arg
- 萨嘟嘟嘟嘟的打算的发
- sendto发送UDP包的时候,如果发不出去,可能是没有bind的原因
- android 数据连接关闭的情况下如何让彩信发不出去
- Android 数据连接关闭的情况下如何让彩信发不出去
- Objective-C Runtime
- WebService:CXF开发
- pg_dump和pg_restore
- SIMLOCK feature
- [DP] Minimum Path Sum
- spring constructor-arg name UnsatisfiedDependencyException 本来打算发在stack overflow上的,死活发不出去,先记着吧
- Android sdk镜像设置
- 微信支付APP支付完全攻略
- 迭代器(Iterator)
- HDU 1176 二维动态规划 免费馅饼
- Cef设置Cookie
- 转换字符串到整数——LintCode
- java.lang.IllegalArgumentException: Page directive: invalid value for import
- 安卓学习之路3之插件TextView、EditText和ImageView