? ??
@Date
public class Student{public String name ;public String[] array ;public Grade grade;public List list;public Set set;public Map map;public Properties prop;//使用IOC默認使用無參構造器創建對象,沒有無參構造器會報錯 // public Student(String stuName){// this.stuName = stuName;// }public Student(){System.out.println("student被創建。。。")}public void init(){System.out.println("init被創建。。。")}public void destory(){System.out.println("destory被創建。。。")}}@Dateclass Grade{public Integer gid;public String gname;
}
使用Set:需要有對應的set方法
????????<property name="name" value="admin"></property>
? ? ? ? <property name = "array">
? ? ? ? ? ? ? ? <array>
? ? ? ? ? ? ? ? ? ? ? ? <value>11</value>
? ? ? ? ? ? ? ? ? ? ? ? <value>22</value>
? ? ? ? ? ? ? ? ? ? ? ? <ref>grade</ref>
? ? ? ? ? ? ? ? </array>
? ? ? ? </property>
? ? ? ? 注入一個Grade對象 依賴注入在IOC的基礎上? 容器中要有Grade對象
<property name="grade" ref="grade"></property>
<bean id="grade" class="com.xja.bean.Grade">
<property name="map">
? ? ? ? <map>
? ? ? ? ? ? ? ? <entry key="a" value="1">
? ? ? ? ? ? ? ? <entry key="b" value-ref="grade">
? ? ? ? </map>
</property>
<property name="prop">
? ? ? <props>
? ? ? ? ? ? ? ? <prop key="uname">admin</prop>
? ? ? ? ? ? ? ? <prop key="pwd">123</prop>
? ? ? ? </props>
</property>
<property name = "set">
? ? ? ? ? ? ? ? <set>
? ? ? ? ? ? ? ? ? ? ? ? <value>11</value>
? ? ? ? ? ? ? ? ? ? ? ? <value>22</value>
? ? ? ? ? ? ? ? ? ? ????<value>11</value>? ?//set特性不可重復,后添加的無效
? ? ? ? ? ? ? ? </set>
?</property>
使用構造器:? 需要有對應匹配的構造器? ?No matching constructor
Spring依賴注入不同類型
????????name :按照參數名稱
????????index:按照參數的位置 從0開始
????????type:按照參數的類型? 類型有多個就設置多個
????????<constructor-arg name="name" value="rk">? </constructor-arg>