spring constructor-arg name UnsatisfiedDependencyException 本来打算发在stack overflow上的,死活发不出去,先记着吧
2015-12-14 14:56
471 查看
When I use Spring framework, I encountered a problem:
The stack trace like this:
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.
相关文章推荐
- JavaNote
- Java类文件结构
- JAVA-适配器模式
- Spring3 Schedule Task之注解实现 (两次起动Schedule Task 的解决方案)
- Netbeans 自带注释插件的使用
- Animation的二种使用方法一种是JavaCode一种是xml文件配置
- 使用比较器让对象进行排序
- Java正则表达式入门
- java使用replaceall中的正则表达式剔除掉img标签里的class
- spring的学习笔记
- eclipse修改web项目名称
- java的jdk安装和切换
- 阅读Netty实战(精髓)笔记
- 分布式搜索elasticsearch java API
- Java无符号数
- struts+hibernate整合报错
- Spring框架学习之一基础概念了解
- 过来人的Java编程经验汇总3-整理&重制
- 过来人的Java编程经验汇总2-整理&重制
- struts.xml 重定向url