1. 程式人生 > >Spring中各種依賴注入的程式碼實現

Spring中各種依賴注入的程式碼實現

DI:Spring稱之為依賴注入,就是維護物件和物件之間的關係
通俗的講,就是 給類的成員賦值。
1、屬性賦值:

class A{
String str = “Hello  Word !”
}
注入:
<bean id="A" class="類A的全稱">
        <property name="str" value="Hello  Word"></property>
</bean>

2、依賴注入的幾種方法:

A. 構造器注入
B. Setter方法注入
C. 方法注入

3、構造器注入:
通過構造方法給屬性賦值

 <bean
id="stu" class="cn.gson.springmavenTest.pojos.Student">
<!-- 給構造方法的第一個引數賦值 --> <constructor-arg index="0" value="0"></constructor-arg> <!-- 給構造方法的第二個引數賦值 --> <constructor-arg index="1" value="Hello"> </constructor-arg> </bean
>

4、Setter方法注入:

<bean id="stu"            class="cn.gson.springmavenTest.pojos.Student">
        <property name="sid" value="10"></property>
        <property name="name" value="laobiao"></property>
 </bean>

強調:name不是屬性名,而是Setter方法的後半部分
注意:該類必須要有公共的無參構造方法
屬性為private的訪問級別,不建議為public
屬性必要時通過一組Setter和getter方法來訪問

5、注入常量

<property name="index" value="10"/> 
或者是:
<property name="index">
<value>10</index>
<property/>

6、注入List集合,使用list標籤

<property name="luckColor">
            <list>
                <value>紅色</value>
                <value>黃色</value>
                <value>藍色</value>
            </list>
</property>

7、注入Set集合,使用Set標籤

<property name="luckColor">
            <set>
                <value>紅色</value>
                <value>黃色</value>
                <value>藍色</value>
            </set>
</property>

8、注入Map集合,鍵和值物件可以是任意型別,使用map標籤

<property name="lockGril">
        <map>
        <entry key="1" value="楊冪"></entry>
        <entry key="2" value="趙麗穎"></entry>
        <entry key="3" value="王麗坤"></entry>
        </map>
</property>

9、注入陣列

<property name="luckNumber">
            <array>
                <value>3</value>
                <value>19</value>
            </array>
</property>

10、注入其他物件(必須是Spring容器中的物件)
Teacher類是Student類中的一個屬性變數

<bean id="stu" class="cn.gson.springmavenTest.pojos.Student">
        <property name="teacher" ref="tea"></property>
</bean>
<bean id="tea" class="cn.gson.springmavenTest.pojos.Teacher">
    <property name="name" value="laobiao"></property>
    <property name="age" value="20"></property>
</bean>

11、使用P名稱空間簡化Setter注入

<bean id="stu" class="cn.gson.springmavenTest.pojos.Student">
        <property name="teacher" ref="tea"></property>
</bean>
    <bean id="tea"   
    class="cn.gson.springmavenTest.pojos.Teacher"
    p:name="laobiao" p:age="20" 
    p:birthday-ref="date"
    </bean>
    <bean id="date" class="java.util.Date">

    </bean>

12、零配置
@Autowired:按照型別進行自動裝配

@Component("stu")
public class Student {

    private long sid;
    private String name;
    @Autowired(required=true)
    private Teacher teacher;
    private List<String> luckColor;
    private Set<String> luckCity;
    private Map<Integer,String> lockGril;
    private int[] luckNumber;
}

@Autowired(required=false)當根據型別去找,找不到時,注入一個空。
@Autowired(required=true)當根據型別去找,找不到時,丟擲異常資訊。
@Qualifier(“tea”):根據名稱進行裝配,相當於在配置的時候的stu裡面的Tea的id

@Component("stu")
public class Student {

    private long sid;
    private String name;
    @Autowired
    @Qualifier("tea") //根據名稱進行裝配
    private Teacher teacher;
}

後臺測試:首先得到核心配置檔案,比如:applicationContext.xml

ApplicationContext ac = 
            new ClassPathXmlApplicationContext("appaicationContext.xml");
    //方式一
    Student student = (Student) ac.getBean("stu");
    //方式二
    //Student student = ac.getBean(Student.class);
    //方式三
   // Student student =  ac.getBean("stu", Student.class);
    System.out.println(student.sayHello("zhagnxusheng"));
    //System.out.println(student.getName()+student.getSid());

就會發現,和以前最大的不同點就是不需要去new 這個Student,就獲取它的屬性和方法
最後,還可以進:註解和配置混合注入