Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1036868
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1701
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:06
文章分类
文章存档

2014年(19)

2013年(27)

分类: 网络与安全

2014-01-06 11:32:33

1、说明

   constructor-arg:通过构造函数注入。
   property:通过setter对应的方法注入。


2、constructor-arg的使用示例

   (1)、Model代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    
publicclassStudent {
privateInteger id;
privateString name;
privateList dream;
privateMap score;
privatebooleangraduation;
publicStudent() {
}
publicStudent(Integer id, String name, List dream,
Map 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配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    





soldier
scientist
pilot











说明:也可以改成方式;boolean的值既可以用0/1填充,也可以用true/false填充。


3、property的使用示例

   (1)、Model代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    
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配置:
1
2
3
4
    






4、Test

   (1)、测试代码:
1
2
3
4
5
6
7
8
9
10
    
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]


阅读(2137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~