constructor-arg和property的区别

本文出自 “袭冷” 博客,http://racoguo.blog.51cto.com/2309068/1236379


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
23
24
25
26
27
public  class  Student {
     private  Integer id;
     private  String name;
     private  List<String> dream;
     private  Map<String, Integer> score;
     private  boolean  graduation;
                                                                                                                                                                           
     public  Student() {
                                                                                                                                                                           
     }
                                                                                                                                                                           
     public  Student(Integer id, String name, List<String> dream,
             Map<String, Integer> score,  boolean  graduation) {
         this .id = id;
         this .name = name;
         this .dream = dream;
         this .score = score;
         this .graduation = graduation;
     }
                                                                                                                                                                           
     @Override
     public  String 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
< bean  id = "student"  class = "com.rc.sp.Student" >
     < constructor-arg  name = "id"  value = "1" />
     < constructor-arg  name = "name"  value = "student" />
     < constructor-arg  name = "dream" >
         < list >
             < value >soldier</ value >
             < value >scientist</ value >
             < value >pilot</ value >
         </ list >
     </ constructor-arg >
     < constructor-arg  name = "score" >
         < map >
             < entry  key = "math"  value = "90" />
             < entry  key = "english"  value = "85" />
         </ map >
     </ constructor-arg >
     < constructor-arg  name = "graduation"  value = "false" />
</ bean >

说明:<constructor-arg name="id" value="1"/>也可以改成<constructor-arg index="0" value="1"/>方式;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
21
22
23
24
25
26
public  class  Teacher {
     private  Integer id;
     private  String name;
                                                         
     public  Integer getId() {
         return  id;
     }
                                                         
     public  void  setId(Integer id) {
         this .id = id;
     }
                                                         
     public  String getName() {
         return  name;
     }
                                                         
     public  void  setName(String name) {
         this .name = name;
     }
                                                         
     @Override
     public  String toString() {
         return  "Teacher [id="  + id +  ", name="  + name +  "]" ;
     }
                                                         
}


   (2)、xml配置:

1
2
3
4
< bean  id = "teacher"  class = "com.rc.sp.Teacher" >
     < property  name = "id"  value = "1" ></ property >
     < property  name = "name"  value = "teacher" ></ property >
</ bean >


4、Test

   (1)、测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
public  class  Run {
     public  static  void  main(String[] args) {
         ApplicationContext context =  new  ClassPathXmlApplicationContext(
                 "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]


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值