Spring - constructor-arg和property

一、說明java

   constructor-arg:經過構造函數注入。 
   property:經過setter對應的方法注入。app

 

二、constructor-arg的使用示例ide

   (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填充。this

 

三、property的使用示例spa

   (1)、Model代碼:code

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配置: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 >

 

四、Testci

   (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]

相關文章
相關標籤/搜索