1. 程式人生 > >spring依賴注入方式及其優缺點

spring依賴注入方式及其優缺點

1 public class ExampleBean
2 {
3     private AnotherBean beanOne;
4     private YetAnotherBean beanTwo;
5     private int i;
6     public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean,
                                                                    int i)
7 {
8         this.beanOne = anotherBean;
9         this.beanTwo = yetAnotherBean;
10         this.i = i;
11     }
12 }
13
當構造方法中帶多個不同的基本資料型別的引數時,為了避免產生二義性,可以採用type或者index來指定構造方法的引數的型別和順序。
如:
   type方法
1 <constructor-arg type="int">
2 <value>7500000</value>
3 </constructor-arg>
4   <constructor-arg type="java.lang.String">
5 <value>42</value>
6 </constructor-arg>
7
    index方法
1 <bean id="exampleBean" class="examples.ExampleBean">
2   <constructor-arg index="0">
3 <value>7500000</value>
4 </constructor-arg>
5   <constructor-arg index="1">
6 <value>42</value>
7 </constructor-arg>
8 </bean>
9
總結:
     type1在靈活性、易用性上不如其他兩種注入模式, Type2 和Type3型的依賴注入實現則是目前主流的IOC實現模式, Type3 和Type2模式各有千秋,而Spring都對Type3和