spring 依赖注入方式

来源:互联网 发布:js 添加disable属性 编辑:程序博客网 时间:2024/05/16 08:56
一:依赖注入的方式 
  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.human.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>  

基本类型可以直接使用value属性,如果是符合类型,如list,map,set需要在<map></map>等标签
最后一个参数ifMarried是一个boolean类型的参数,在配置的时候可以是true/false或者0/1; 

二:property的简单使用 
  java代码: 

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

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. </bean>  
    

在这里我配置了一个init-method="init"表示在容器实例化这个doctor的时候,调用一个Doctor类的init方法,本来还以为可以通过这个init方法来注入要注入的信息,但是尝试过后才知道这个init方法是不能带参数的。 
1 0