spring属性注入必须有默认构造方法
来源:互联网 发布:玛祖铭立 知乎 编辑:程序博客网 时间:2024/05/23 23:04
最基本的对象创建方式,只需要有一个无参构造函数(类中没有写任何的构造函数,默认就是有一个构造函数,如果写了任何一个构造函数,默认的无参构造函数就不会自动创建哦!!)和字段的setter方法。
Person类:
package com.mc.base.learn.spring.bean;public class Person { private String name; private Integer id; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "Person [name=" + name + ", id=" + id + "]"; }}
XML配置:
<?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 class="com.mc.base.learn.spring.bean.Person" id="person"> <property name="name" value="LiuChunfu"></property> <property name="id" value="125"></property> </bean></beans>
其本质为:
SpringContext利用无参的构造函数创建一个对象,然后利用setter方法赋值。所以如果无参构造函数不存在,Spring上下文创建对象的时候便会报错。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mc.base.learn.spring.bean.Person]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.mc.base.learn.spring.bean.Person.<init>() at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) 。。。。。
阅读全文
0 0
- spring属性注入必须有默认构造方法
- Spring属性注入和构造器注入、工厂方法注入
- spring构造方法注入
- Spring 属性注入和构造器注入
- spring之构造函数注入属性值与set方法注入属性值
- Spring bean属性注入(构造器)
- spring属性和构造器注入
- Spring(六)属性依赖注入(构造方法,setter方法,p命名空间,spel表达式,集合注入)
- spring注入方式 (Set注入 构造方法注入)
- Spring依赖属性注入和构造函数注入
- spring + maven 属性注入和构造函数注入(1)
- (二)spring的注入---配置构造器属性注入
- spring的属性注入和构造器注入
- spring注入:构造注入
- Spring整理6 -- 构造方法注入
- Spring通过构造方法依赖注入
- spring构造方法注入时间类型问题
- spring构造方法之索引注入
- bzoj 1060: [ZJOI2007]时态同步 树形dp
- pat 乙级 1019. 数字黑洞 (20)
- oracle数据库建立后,用户名跟密码设置windows平台
- Windows显示驱动中读取EDID
- form表单中传过来的参数对应的值有可能是""空串
- spring属性注入必须有默认构造方法
- iOS 3D Touch超详细入门(附带demo示例代码)
- csu1115 最短的名字 字典树模板
- 2017.9.2总结3-分割田地
- css基础(4)
- POJ 3087
- Android 中怎样查找SELinux导致的权限受限有关问题
- 小事: 自动生成Getter代码
- UVA 11624