首頁  >  文章  >  Java  >  spring框架學習(二)依賴注入

spring框架學習(二)依賴注入

黄舟
黄舟原創
2016-12-29 13:10:091315瀏覽

spring框架為我們提供了三種注入方式,分別是set注入,構造方法注入,介面注入。介面注入不作要求,下面介紹前兩種方式。

1,set注入
採用屬性的set方法進行初始化,就成為set注入。
1)給普通字元類型賦值。

public class User{
   privateString username;
 
   publicString getUsername() {
       returnusername;
   }
   publicvoid setUsername(String username) {
      this.username= username;
   }
}

我們只需要提供屬性的set方法,然後去屬性檔案中去設定好讓框架能夠找到applicationContext.xml檔案的beans標籤。標籤beans中加入bean標籤,
指定id,class值,id值不做要求,class值為物件所在的完整路徑。 bean標籤再加入property
標籤,要求,name值與User類別中對應的屬性名稱一致。 value值就是我們要給User類別中的username屬性賦的值。

<bean id="userAction"class="com.lsz.spring.action.User" >
	<property name="username" value="admin"></property>
</bean>

2)給物件賦值
同樣提供物件的set方法

public class User{
     private UserService userservice;
     public UserServicegetUserservice() {
          returnuser;
     }
     public void setUserservice(UserService userservice){
         this.userservice= userservice;
     }
}

設定檔中要增加UserService的bean標籤聲明及User物件對UserService引用。

<!--对象的声明-->
<bean id="userService" class="com.lsz.spring.service.UserService"></bean>
 
<bean id="userAction"class="com.lsz.spring.action.User" >
   <property name="userservice" ref="userService"></property>
</bean>

這樣配置,框架就會將UserService物件注入到User類別中。

3)給list集合賦值
同樣提供set方法

public class User{
    privateList<String> username;
    publicList<String> getUsername() {
        returnusername;
    }
    publicvoid setUsername(List<String> username) {
        this.username= username;
    }
}
<bean id="userAction"class="com.lsz.spring.action.User" >
     <propertyname="username">
           <list>
               <value>zhang,san</value>
               <value>lisi</value>
               <value>wangwu</value>                                
               </list>
    </property>
</bean>

4)給屬性檔中的欄位賦值

public class User{
    privateProperties props ;
    publicProperties getProps() {
        returnprops;
    }
    publicvoid setProps(Properties props) {
        this.props= props;
    }
}
<bean>
    <propertyname="props">
        <props>
           <propkey="url">jdbc:oracle:thin:@localhost:orl</prop>
           <propkey="driverName">oracle.jdbc.driver.OracleDriver</prop>
           <propkey="username">scott</prop>
           <propkey="password">tiger</prop>
        </props>
    </property>
</bean>

標籤中的key值是.properties屬性檔中的名稱

標籤中的key值是.properties屬性檔中的名稱



給什麼賦值,設定檔中標籤的name屬性值一定是跟物件中名稱一致。

2建構方法注入
1)構造方法一個參數

public class User{
    privateString usercode;
    publicUser(String usercode) {
        this.usercode=usercode;
    }
}
<bean id="userAction"class="com.lsz.spring.action.User">                        
    <constructor-argvalue="admin"></constructor-arg>                        
</bean>

2)建構子有兩個參數時

當參數為非字串型別時,在設定檔中需要製定型別,如果不指定型別一律依照字串型別賦值。

當參數類型不一致時,框架是按照字串的類型進行查找的,因此需要在配置文件中製定是參數的位置

<constructor-argvalue="admin"index="0"></constructor-arg>                
<constructor-argvalue="23" type="int"index="1"></constructor-arg>

這樣制定,就是構造函數中,第一個參數為string類型,第二個參數為int型別


 以上就是spring框架學習(二)依賴注入的內容,更多相關內容請關注PHP中文網(www.php.cn)!

🎜🎜🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn