Spring——多種方式實現依賴注入


  在Spring的XML配置中,只有一種聲明bean的方式:使用<bean>元素並指定class屬性。Spring會從這里獲取必要的信息來創建bean。

但是,在XML中聲明DI時,會有多種可選的配置方式和風格,具體到setter方式、構造器方式和P命名空間方式。

一、使用setter進行注入

   

 

 二、使用構造注入 

  • 為POJO類創建帶參的構造方法
  • 使用<constructor-arg>元素

  給原先的實體類添加構造,記得帶着無參構造,這個問題在這里不細說

  

  Spring配置文件如下

<bean id="student" class="com.cmy.entity.StudentBean">
    <!--constructor-arg:代表構造函數中的一個參數 索引從0開始-->
    <constructor-arg type="java.lang.String" value="呵呵"></constructor-arg>
    <constructor-arg type="int" value="16"></constructor-arg>
</bean>

  注意:

     1、一個<constructor-arg>元素表示構造方法的一個參數,且使用時不區分順序。

     2、通過<constructor-arg>元素的index 屬性可以指定該參數的位置索引,位置 從0 開始。

     3、<constructor-arg>元素還提供了type 屬性用來指定參數的類型,避免字符串和基本數據類型的混淆。

三、使用P命名空間進行注入

<bean id="student" class="com.cmy.entity.studentBean" p:name="呵呵" 
      p:age="16">
</bean>

  注意:

    1、P命名空間注入的方式也是使用setter方式注入,所有POJO類中必須存在對應的set方法

    2、 使用前需要在Spring的配置文件中引入p命名空間 xmlns:p="http://www.springframework.org/schema/p"

      AOP也同樣如此,不過在idea中可以使用ctrl+Enter自動引入  xmlns:aop="http://www.springframework.org/schema/aop"

四、注入不同的類型

  

 

   這個關注第四點,POJO類如下:

public class Datetype {
    //數組
    private  String[]   arrays;
    //list
    private List<Integer> lists;
    //set
    private Set<Integer> sets;
    //map
    private Map<String,String> maps;
    //配置
    private Properties pros;

    public String[] getArrays() {
        return arrays;
    }

    public void setArrays(String[] arrays) {
        this.arrays = arrays;
    }

    public List<Integer> getLists() {
        return lists;
    }

    public void setLists(List<Integer> lists) {
        this.lists = lists;
    }

    public Set<Integer> getSets() {
        return sets;
    }

    public void setSets(Set<Integer> sets) {
        this.sets = sets;
    }

    public Map<String, String> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public Properties getPros() {
        return pros;
    }

    public void setPros(Properties pros) {
        this.pros = pros;
    }

    @Override
    public String toString() {
        return "Datetype{" +
                "arrays=" + Arrays.toString(arrays) +
                ", lists=" + lists +
                ", sets=" + sets +
                ", maps=" + maps +
                ", pros=" + pros +
                '}';
    }
}

  大配置文件:

<bean id="dateType" class="cn.spring.entity.Datetype">

        <property name="arrays">
            <array>
                <value>hehe</value>
                <value>haha</value>
            </array>
        </property>

        <property name="lists">
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </property>

        <property name="sets">
            <set>
                <value>3</value>
                <value>4</value>
            </set>
        </property>

        <property name="maps">
            <map>
                <entry key="c" value="才"></entry>
                <entry value="的" key="d"></entry>
            </map>
        </property>

        <property name="pros">
            <props>
                <prop key="a">啊</prop>
                <prop key="b">吧</prop>
            </props>
        </property>

    </bean>

  測試類:

      

  輸出結果:

    

 

 五、域屬性的自動注入

   在之前我們采用手動的域屬性注入,即:

   

 

   這里采用自動注入,自動注入有兩種方式:

  1.byName:保證域屬性名字和bean的id一致

  2.byType:保證域屬性的類型和bean的類型一致,與其相兼容的類型也不可以

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM