constructor-arg的使用以及Properties的使用

来源:互联网 发布:竹村桐子知乎 编辑:程序博客网 时间:2024/06/03 20:12
  1. constructor-arg的使用:通过构造函数注入

  2. public class Man {  
  3.   
  4. private String name ;  
  5. private int age;  
  6. private List hobby;  
  7. private Map  friends;  
  8. private Set  set;  
  9. private boolean ifMarried;  
  10.   
  11. public Man() {  
  12.       
  13. }  
  14.   
  15.    public Man(String name, int age,List hobby,Map friends,Set    set,boolean ifMarried){  
  16.     this.name = name;  
  17.     this.age = age;  
  18.     this.hobby = hobby;  
  19.     this.friends = friends;  
  20.     this.set = set;  
  21.     this.ifMarried = ifMarried;  
  22.    }  
  23.      
  24.    public String getInfo(){  
  25.       
  26.     String info = "姓名:"+this.name+"\n年龄:"+this.age+"\n爱好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;  
  27.        return info;  
  28.    }  
  29. 然后进行XML的配置:
    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>  

        1. Properties的使用:通过setter方法注入
        2. 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. }  
      36. 相应的XML文件:
        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. /bean> 



0 0
原创粉丝点击