Spring的兩種依賴注入方式:setter方法注入與構造方法注入

 Spring的兩種依賴注入方式:setter注入與構造方法注入,這兩種方法的不同主要就是在xml文件下對應使用property和constructor-arg屬性, 例如:

property屬性:<property name="id" value="123"></property>(其中name的值爲原類中的屬性名)

constructor-arg屬性:<constructor-arg index="0" value="456"></constructor-arg>(其中index的值爲0~n-1,n代表構造函數中的輸入參數的數量)


1.setter方法注入

   setter方法注入即是創建一個普通的JavaBean類,爲需要注入的屬性通過對應的setter方法即可,如:

(1)創建一個Id類:

[java] view plain copy
  1. package com.loster.li;  
  2.   
  3. public class Id {  
  4.     private int id;  
  5.     private String name;  
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18. }  
(2)創建配置文件Id_Bean.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.              
  7.     <bean id="id" class="com.loster.li.Id">  
  8.     <property name="id" value="123"></property>  
  9.     <property name="name" value="xiaoli"></property>  
  10.     </bean>  
  11. </beans>  
(3)編寫測試類IdTest.java,並運行:
[java] view plain copy
  1. package com.loster.li;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class IdTest {  
  6.     public static void main(String[] args){  
  7.         ClassPathXmlApplicationContext context = new   
  8.                 ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");  
  9.           
  10.         Id id = (Id)context.getBean("id");  
  11.         System.out.println(id.getId());  
  12.         System.out.println(id.getName());  
  13.     }  
  14. }  
   運行結果如下:



2.構造方法注入

(1)將上面的Id.class修改爲:

[java] view plain copy
  1. package com.loster.li;  
  2.   
  3. public class Id {  
  4.     private int id;  
  5.     private String name;  
  6.     public Id(int id,String name){  
  7.         this.id = id;  
  8.         this.name = name;  
  9.     }  
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(int id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22. }  
(2)將上面的Id_Bean.xml修改爲:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.              
  7.     <bean id="id" class="com.loster.li.Id">  
  8.     <constructor-arg index="0" value="456"></constructor-arg>  
  9.     <constructor-arg index="1" value="dawang"></constructor-arg>  
  10.     </bean>  
  11. </beans>  
(3)再次運行IdTest.java,運行結果如下:

                                                                                                                    





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章