property和constructor-arg的使用

一:依赖注入的方式 
  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.    

  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>  


最后一个参数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. }  

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>  

你可能感兴趣的:(spring,依赖注入,property,constructor-arg)