property和constructor-arg的使用

來源:互聯網
上載者:User
一:依賴注入的方式   constructor-arg:通過建構函式注入。
  property:通過setxx方法注入。

二:constructor-arg的簡單使用
  java代碼
 

Java代碼
 
  1.    public class Man {   
  2.   
  3. private String name ;   
  4. private int age;   
  5. private List hobby;   
  6. private Map  friends;   
  7. private Set  set;   
  8. private boolean ifMarried;   
  9.   
  10. public Man() {   
  11.        
  12. }   
  13.   
  14.    public Man(String name, int age,List hobby,Map friends,Set    set,boolean ifMarried){   
  15.     this.name = name;   
  16.     this.age = age;   
  17.     this.hobby = hobby;   
  18.     this.friends = friends;   
  19.     this.set = set;   
  20.     this.ifMarried = ifMarried;   
  21.    }   
  22.       
  23.    public String getInfo(){   
  24.        
  25.     String info = "姓名:"+this.name+"\n年齡:"+this.age+"\n愛好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
      
  26.        return info;   
  27.    }   
  28.   
  29.   
  30.   
  31.    
    public class Man {private String name ;private int age;private List hobby;private Map  friends;private Set  set;private boolean ifMarried;public Man() {}    public Man(String name, int age,List hobby,Map friends,Set    set,boolean ifMarried){    this.name = name;    this.age = age;    this.hobby = hobby;    this.friends = friends;    this.set = set;    this.ifMarried = ifMarried;    }        public String getInfo(){        String info = "姓名:"+this.name+"\n年齡:"+this.age+"\n愛好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;        return info;    }}   

  xml設定檔 Java代碼
 

  1. <bean id="man" class="com.spring.test.man.Man">   
  2.    <constructor-arg value="zzy" index="0" >   
  3.    </constructor-arg>   
  4.       
  5.    <constructor-arg value="10" index="1">   
  6.    </constructor-arg>   
  7.      
  8.    <constructor-arg>   
  9.      <list>   
  10.         <value>movie</value>   
  11.         <value>music</value>   
  12.      </list>   
  13.    </constructor-arg>   
  14.       
  15.    <constructor-arg>   
  16.       <set>   
  17.          <value>Lady is GaGa</value>   
  18.          <value>GaGa is Lady</value>   
  19.       </set>   
  20.    </constructor-arg>   
  21.   
  22.    <constructor-arg>   
  23.        <map>   
  24.           <entry key="liuhua" value="man"></entry>   
  25.           <entry key="xujinglei" value="female"></entry>   
  26.        </map>   
  27.    </constructor-arg>   
  28.       
  29.    <constructor-arg index="5" value="0">   
  30.    </constructor-arg>   
  31. </bean>  
<bean id="man" class="com.spring.test.man.Man">   <constructor-arg value="zzy" index="0" >   </constructor-arg>      <constructor-arg value="10" index="1">   </constructor-arg>     <constructor-arg>     <list>        <value>movie</value>        <value>music</value>     </list>   </constructor-arg>      <constructor-arg>      <set>         <value>Lady is GaGa</value>         <value>GaGa is Lady</value>      </set>   </constructor-arg>   <constructor-arg>       <map>          <entry key="liuhua" value="man"></entry>          <entry key="xujinglei" value="female"></entry>       </map>   </constructor-arg>      <constructor-arg index="5" value="0">   </constructor-arg></bean>

最後一個參數ifMarried是一個boolean類型的參數,在配置的時候可以是true/false或者0/1;

二:property的簡單使用
  java代碼:

  

Java代碼
 
  1. public class Doctor {   
  2.   
  3.     private String name;   
  4.     private String sex;   
  5.        
  6.        
  7.     public String getName() {   
  8.         return name;   
  9.     }   
  10.   
  11.   
  12.     public void setName(String name) {   
  13.         this.name = name;   
  14.     }   
  15.   
  16.        
  17.   
  18.     public String getSex() {   
  19.         return sex;   
  20.     }   
  21.   
  22.   
  23.     public void setSex(String sex) {   
  24.         this.sex = sex;   
  25.     }   
  26.   
  27.   
  28.     public void init(){   
  29.         System.out.println("88888888888");   
  30.     }   
  31.     public void init(String name,String sex){   
  32.         this.name = name;   
  33.         this.sex = sex;   
  34.     }   
  35. }  
public class Doctor {private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public void init(){System.out.println("88888888888");}public void init(String name,String sex){this.name = name;this.sex = sex;}}

xml設定檔: Java代碼
 

  1. <bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">   
  2.  <property name="name" value="doctor"></property>   
  3.  <property name="sex" value="i don't know"></property>   
  4. lt;/bean>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.