spring错误:bean实例类中的属性类型与spring配置文件中的构造器属性类型不对应
2018-02-11 21:49
555 查看
错误原因:bean实例类中的属性类型与spring配置文件中的构造器属性类型不对应所导致,这种情况多出在使用类型指定构造器参数;org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userId' defined in class path resource [com/java/f_xml/a_constructor/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?Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userId' defined in class path resource [com/java/f_xml/a_constructor/beans.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments? 这种错误是因为构造器中的type属性不会自动对应拆箱装箱属性类型,简单点说就是类中使用的是基本数据类型,配置文件中对应的type属性值就要是基本数据类型;类中使用的是类类型,配置文件中对应的type属性值就要是包名加上类类型;public class User { private String username; private int id;//可以为private Integer id;对应的配置文件type="java.lang.Integer",返回数据库字段值是null的话,int类型会报错。int是基本数据类型,其声明的是变量,而null则是对象。所以建议用integer; private int age; public User(int id, int age) { super(); this.id = id; this.age = age; } public User(String username, int id) { super(); this.username = username; this.id = id; } }xml:<bean id="userId" class="com.java.f_xml.a_constructor.User" > <constructor-arg index="0" type="java.lang.String" value="jack"></constructor-arg> <constructor-arg index="1" type="int" value="123"></constructor-arg> </bean>
参考文档:http://blog.csdn.net/u013493841/article/details/51944022https://www.2cto.com/kf/201305/211728.html
参考文档:http://blog.csdn.net/u013493841/article/details/51944022https://www.2cto.com/kf/201305/211728.html
相关文章推荐
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- Spring 配置文件中Bean 属性id和name的区别
- Spring配置文件加载时出现Bean property 'newscontentDAO' is not writable or has an invalid setter method错误的解决[00原创]
- spring配置文件中<bean>的id和name属性区别,以及identifier和aliases
- Spring 配置文件 属性 ref bean和local的区别
- spring配置文件中bean的属性name与id的区别
- Spring-配置文件Bean定义中parent属性详解
- Spring-Bean配置-使用外部属性文件
- Spring4笔记----使用外部属性文件配置 bean
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- 升级spring1.2.8.jar到 spring2.5.jar对应的bean.xml文件没做升级出现的错误
- Spring-配置文件Bean定义中parent属性详解
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- Spring 配置文件 属性 ref bean和local的区别
- Spring-配置文件Bean定义中parent属性详解
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- Spring-配置文件Bean定义中parent属性详解