<学习笔记>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>  
    

在这里我配置了一个init-method="init"表示在容易实例化这个doctor的时候,调用一个Doctor类的init方法,本来还以为可以通过这个init方法来注入要注入的信息,但是尝试过后才知道这个init方法是不能带参数的。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值