1. 程式人生 > 其它 >Spring全解-03-依賴注入

Spring全解-03-依賴注入

依賴注入(DI,Dependency Injection)

依賴:指Bean物件的建立依賴於容器。

注入:指Bean物件所依賴的資源,由容器來設定和裝配。

構造器注入前面已經瞭解

set注入,重點

想要使用Set注入,要求被注入的屬性,必須有set方法。

測試

 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         
this.address = address; } }
 package com.li.pojo;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
 public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     
private Map<String,String> card; private Set<String> games; private String wife; private Properties info; public void setName(String name) { this.name = name; } public void setAddress(Address address) { this.address = address; }
public void setBooks(String[] books) { this.books = books; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public void setCard(Map<String, String> card) { this.card = card; } public void setGames(Set<String> games) { this.games = games; } public void setWife(String wife) { this.wife = wife; } public void setInfo(Properties info) { this.info = info; } public void show(){ System.out.println("name="+ name + ",address="+ address.getAddress() + ",books=" ); for (String book:books){ System.out.print("<<"+book+">>\t"); } System.out.println("\n愛好:"+hobbys); System.out.println("card:"+card); System.out.println("games:"+games); System.out.println("wife:"+wife); System.out.println("info:"+info); } }

常量注入

 <bean id="student" class="com.li.pojo.Student">
     <property name="name" value="李扶搖"/>
 </bean>

Bean注入

注意,注入bean不是使用value,而是引用 ref

 <bean id="addr" class="com.li.pojo.Address">
     <property name="address" value="江西"/>
 </bean>
 
 <bean id="student" class="com.li.pojo.Student">
     <property name="name" value="李扶搖"/>
     <property name="address" ref="addr"/>
 </bean>

陣列注入

 <bean id="student" class="com.li.pojo.Student">
     <property name="name" value="李扶搖"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>西遊記</value>
             <value>紅樓夢</value>
             <value>水滸傳</value>
         </array>
     </property>
 </bean>

List注入

 <property name="hobbys">
     <list>
         <value>打球</value>
         <value>打遊戲</value>
         <value>看書</value>
     </list>
 </property>

Map注入

 <property name="card">
     <map>
         <entry key="中國郵政" value="456456456465456"/>
         <entry key="建設" value="1456682255511"/>
     </map>
 </property>

set注入

 <property name="games">
     <set>
         <value>LOL</value>
         <value>BOB</value>
         <value>COC</value>
     </set>
 </property>

Null注入

 <property name="wife"><null/></property>

Properties注入

 <property name="info">
     <props>
         <prop key="學號">20210701</prop>
         <prop key="性別"></prop>
         <prop key="姓名">李扶搖</prop>
     </props>
 </property>

p命名和c命名注入

 public class User {
     private String name;
     private int age;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAge(int age) {
         this.age = age;
    }
 
     @Override
     public String toString() {
         return "User{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
    }
 }

注意:使用p命名或者c命名注入時需要在標頭檔案中加入約束條件

 匯入約束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(屬性: properties)名稱空間 , 屬性依然要設定set方法-->
 <bean id="user" class="com.li.pojo.User" p:name="李扶搖" p:age="18"/>
 匯入約束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(構造: Constructor)名稱空間 , 屬性依然要設定set方法-->
 <bean id="user" class="com.li.pojo.User" c:name="李扶搖" c:age="18"/>
 @Test
 public void test02(){
     ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     System.out.println(user);
 }

執行之後,發現程式爆紅

原因:實體類沒有有參構造,即這底層還是構造器注入。

Bean的作用域

Singleton

當一個bean的作用域為Singleton,那麼Spring IoC容器中只會存在一個共享的bean例項,並且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一例項。Singleton是單例型別,就是在建立起容器時就同時自動建立了一個bean的物件,不管你是否使用,他都存在了,每次獲取到的物件都是同一個物件。注意,Singleton作用域是Spring中的預設作用域。

 <bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">

Prototype

當一個bean的作用域為Prototype,表示一個bean定義對應多個物件例項。Prototype作用域的bean會導致在每次對該bean請求(將其注入到另一個bean中,或者以程式的方式呼叫容器的getBean()方法)時都會建立一個新的bean例項。Prototype是原型型別,它在我們建立容器的時候並沒有例項化,而是當我們獲取bean的時候才會去建立一個物件,而且我們每次獲取到的物件都不是同一個物件。根據經驗,對有狀態的bean應該使用prototype作用域,而對無狀態的bean則應該使用singleton作用域。

 <bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>  
  或者
 <bean id="account" class="com.foo.DefaultAccount" singleton="false"/>

Request

當一個bean的作用域為Request,表示在一次HTTP請求中,一個bean定義對應一個例項;即每個HTTP請求都會有各自的bean例項,它們依據某個bean定義建立而成。該作用域僅在基於web的Spring ApplicationContext情形下有效。針對每次HTTP請求,Spring容器會根據loginAction bean的定義建立一個全新的LoginAction bean例項,且該loginAction bean例項僅在當前HTTP request內有效,因此可以根據需要放心的更改所建例項的內部狀態,而其他請求中根據loginAction bean定義建立的例項,將不會看到這些特定於某個請求的狀態變化。當處理請求結束,request作用域的bean例項將被銷燬。

 <bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>

Session

當一個bean的作用域為Session,表示在一個HTTP Session中,一個bean定義對應一個例項。該作用域僅在基於web的Spring ApplicationContext情形下有效。針對某個HTTP Session,Spring容器會根據userPreferences bean定義建立一個全新的userPreferences bean例項,且該userPreferences bean僅在當前HTTP Session內有效。與request作用域一樣,可以根據需要放心的更改所建立例項的內部狀態,而別的HTTP Session中根據userPreferences建立的例項,將不會看到這些特定於某個HTTP Session的狀態變化。當HTTP Session最終被廢棄的時候,在該HTTP Session作用域內的bean也會被廢棄掉。

 <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>