Spring - constructor-arg和property的使用示例
2014-01-05 17:25
274 查看
1、说明
constructor-arg:通过构造函数注入。
property:通过setter对应的方法注入。
2、constructor-arg的使用示例
(1)、Model代码:
(2)、xml配置:
3、property的使用示例
(1)、Model代码:
(2)、xml配置:
4、Test
(1)、测试代码:
(2)、输出结果:
Student [id=1, name=student, dream=[soldier, scientist, pilot],
score={math=90, english=85}, graduation=false]
Teacher [id=1, name=teacher]
本文出自 “袭冷” 博客,请务必保留此出处http://xilen.blog.51cto.com/8455881/1348739
constructor-arg:通过构造函数注入。
property:通过setter对应的方法注入。
2、constructor-arg的使用示例
(1)、Model代码:
publicclassStudent { privateInteger id; privateString name; privateList<String> dream; privateMap<String, Integer> score; privatebooleangraduation; publicStudent() { } publicStudent(Integer id, String name, List<String> dream, Map<String, Integer> score, booleangraduation) { this.id = id; this.name = name; this.dream = dream; this.score = score; this.graduation = graduation; } @Override publicString toString() { return"Student [id="+ id + ", name="+ name + ", dream="+ dream + ", score="+ score + ", graduation="+ graduation + "]"; } }
(2)、xml配置:
<beanid="student"class="com.rc.sp.Student"> <constructor-argname="id"value="1"/> <constructor-argname="name"value="student"/> <constructor-argname="dream"> <list> <value>soldier</value> <value>scientist</value> <value>pilot</value> </list> </constructor-arg> <constructor-argname="score"> <map> <entrykey="math"value="90"/> <entrykey="english"value="85"/> </map> </constructor-arg> <constructor-argname="graduation"value="false"/> </bean>说明:
<
constructor-arg
name
=
"id"
value
=
"1"
/>也可以改成<[code]constructor-arg
index
=
"0"
value
=
"1"
/>方式;boolean的值既可以用0/1填充,也可以用true/false填充。
3、property的使用示例
(1)、Model代码:
publicclassTeacher { privateInteger id; privateString name; publicInteger getId() { returnid; } publicvoidsetId(Integer id) { this.id = id; } publicString getName() { returnname; } publicvoidsetName(String name) { this.name = name; } @Override publicString toString() { return"Teacher [id="+ id + ", name="+ name + "]"; } }
(2)、xml配置:
<beanid="teacher"class="com.rc.sp.Teacher"> <propertyname="id"value="1"></property> <propertyname="name"value="teacher"></property> </bean>
4、Test
(1)、测试代码:
publicclassRun { publicstaticvoidmain(String[] args) { ApplicationContext context = newClassPathXmlApplicationContext( "applicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student); Teacher teacher = (Teacher) context.getBean("teacher"); System.out.println(teacher); } }
(2)、输出结果:
Student [id=1, name=student, dream=[soldier, scientist, pilot],
score={math=90, english=85}, graduation=false]
Teacher [id=1, name=teacher]
本文出自 “袭冷” 博客,请务必保留此出处http://xilen.blog.51cto.com/8455881/1348739
相关文章推荐
- Spring - constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- [JAVA][Spring]Spring2 constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- spring - property和constructor-arg的使用
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
- spring - property和constructor-arg的使用
- Spring : constructor-arg property 注入
- property和constructor-arg的使用
- spring - constructor-arg 的使用
- spring - constructor-arg 的使用
- 使用spring工厂读取property配置文件示例代码
- property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg的使用
- spring配置constructor-arg就报错,property就通过
- Spring使用xml配置bean,主要是constructor-arg