DI依賴注入

1、概念

  • 依賴注入(Dependency Injection,DI)。
  • 依賴 : 指Bean對象的創(chuàng)建依賴于容器 . Bean對象的依賴資源 .
  • 注入 : 指Bean對象所依賴的資源 , 由容器來設(shè)置和裝配 .

2、構(gòu)造器注入

3、set注入

要求被注入的屬性 , 必須有set方法 , set方法的方法名由set + 屬性首字母大寫 , 如果屬性是boolean類型 , 沒有set方法 , 是 is .

測試pojo類 :

  • Address.java
 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }
  • Student.java
 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);
 
    }
 }

1、常量注入

 <bean id="student" class="com.nie.pojo.Student">
     <property name="name" value="小明"/>
 </bean>

測試:

 @Test
 public void test01(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
     Student student = (Student) context.getBean("student");
 
     System.out.println(student.getName());
 
 }

2、Bean注入

注意點(diǎn):這里的值是一個引用,ref

    <bean id="address" class="com.nie.pojo.Address">
        <property name="address" value="浙江"/>

    </bean>
 
 <bean id="student" class="com.nie.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
 </bean>

3、數(shù)組注入

<!--        數(shù)組注入,ref-->
        <property name="books">
            <array>
                <value>春秋</value>
                <value>三國</value>
                <value>西游</value>
            </array>
        </property>

4、List注入

<!--        list-->
        <property name="hobby">
            <list>
                <value>吃飯</value>
                <value>睡覺</value>
                <value>打游戲</value>
            </list>
        </property>

5、Map注入

<!--        map-->
        <property name="card">
            <map>
                <entry key="身份證" value="340827200012096512"/>
                <entry key="銀行卡" value="111111111111111111"/>
            </map>
        </property>

6、set注入

<!--        set-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>cf</value>
            </set>

7、Null注入

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

8、Properties注入

<!--       properties-->
        <property name="info">
            <props>
                <prop key="學(xué)號">1190113016</prop>
                <prop key="性別">男</prop>
                <prop key="username">111</prop>
                <prop key="password">222</prop>
            </props>
        </property>

測試結(jié)果:

image-20211021103840176

9、p命名和c命名注入

  • User.java :【注意:這里沒有有參構(gòu)造器!】
 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命名空間注入 : 需要在頭文件中加入約束文件
 導(dǎo)入約束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(屬性: properties)命名空間 , 屬性依然要設(shè)置set方法-->
 <bean id="user" class="com.nie.pojo.User" p:name="聶" p:age="18"/>
  • c 命名空間注入 : 需要在頭文件中加入約束文件
 導(dǎo)入約束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(構(gòu)造: Constructor)命名空間 , 屬性依然要設(shè)置set方法-->
 <bean id="user" class="com.nie.pojo.User" c:name="聶" c:age="18"/>

發(fā)現(xiàn)問題:爆紅了,剛才我們沒有寫有參構(gòu)造!

解決:把有參構(gòu)造器加上,這里也能知道,c 就是所謂的構(gòu)造器注入!

測試代碼:

 @Test
 public void test02(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     System.out.println(user);
 }

4、bean的作用域

參考:官方文檔1.5:Core Technologies (spring.io)

image-20211021004148410

1、 Singleton

image-20211021104928926

當(dāng)一個bean的作用域?yàn)镾ingleton,那么Spring IoC容器中只會存在一個共享的bean實(shí)例,并且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一實(shí)例。Singleton是單例類型,就是在創(chuàng)建起容器時就同時自動創(chuàng)建了一個bean的對象,不管你是否使用,他都存在了,每次獲取到的對象都是同一個對象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中將bean定義成singleton,可以這樣配置:

 <bean id="ServiceImpl" class="com.nie.service.ServiceImpl" scope="singleton">

測試:

 @Test
 public void test03(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     User user2 = (User) context.getBean("user");
     System.out.println(user==user2);
 }

2、Prototype

image-20211021104944509

當(dāng)一個bean的作用域?yàn)镻rototype,表示一個bean定義對應(yīng)多個對象實(shí)例。Prototype作用域的bean會導(dǎo)致在每次對該bean請求(將其注入到另一個bean中,或者以程序的方式調(diào)用容器的getBean()方法)時都會創(chuàng)建一個新的bean實(shí)例。Prototype是原型類型,它在我們創(chuàng)建容器的時候并沒有實(shí)例化,而是當(dāng)我們獲取bean的時候才會去創(chuàng)建一個對象,而且我們每次獲取到的對象都不是同一個對象。根據(jù)經(jīng)驗(yàn),對有狀態(tài)的bean應(yīng)該使用prototype作用域,而對無狀態(tài)的bean則應(yīng)該使用singleton作用域。在XML中將bean定義成prototype,可以這樣配置:

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

3、Request

當(dāng)一個bean的作用域?yàn)镽equest,表示在一次HTTP請求中,一個bean定義對應(yīng)一個實(shí)例;即每個HTTP請求都會有各自的bean實(shí)例,它們依據(jù)某個bean定義創(chuàng)建而成。該作用域僅在基于web的Spring ApplicationContext情形下有效??紤]下面bean定義:

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

針對每次HTTP請求,Spring容器會根據(jù)loginAction bean的定義創(chuàng)建一個全新的LoginAction bean實(shí)例,且該loginAction bean實(shí)例僅在當(dāng)前HTTP request內(nèi)有效,因此可以根據(jù)需要放心的更改所建實(shí)例的內(nèi)部狀態(tài),而其他請求中根據(jù)loginAction bean定義創(chuàng)建的實(shí)例,將不會看到這些特定于某個請求的狀態(tài)變化。當(dāng)處理請求結(jié)束,request作用域的bean實(shí)例將被銷毀。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容