一:依賴注入的方式 constructor-arg:通過建構函式注入。
property:通過setxx方法注入。
二:constructor-arg的簡單使用
java代碼
Java代碼
- public class Man {
-
- private String name ;
- private int age;
- private List hobby;
- private Map friends;
- private Set set;
- private boolean ifMarried;
-
- public Man() {
-
- }
-
- public Man(String name, int age,List hobby,Map friends,Set set,boolean ifMarried){
- this.name = name;
- this.age = age;
- this.hobby = hobby;
- this.friends = friends;
- this.set = set;
- this.ifMarried = ifMarried;
- }
-
- public String getInfo(){
-
- String info = "姓名:"+this.name+"\n年齡:"+this.age+"\n愛好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
- return info;
- }
-
-
-
-
public class Man {private String name ;private int age;private List hobby;private Map friends;private Set set;private boolean ifMarried;public Man() {} public Man(String name, int age,List hobby,Map friends,Set set,boolean ifMarried){ this.name = name; this.age = age; this.hobby = hobby; this.friends = friends; this.set = set; this.ifMarried = ifMarried; } public String getInfo(){ String info = "姓名:"+this.name+"\n年齡:"+this.age+"\n愛好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set; return info; }}
xml設定檔 Java代碼
- <bean id="man" class="com.spring.test.man.Man">
- <constructor-arg value="zzy" index="0" >
- </constructor-arg>
-
- <constructor-arg value="10" index="1">
- </constructor-arg>
-
- <constructor-arg>
- <list>
- <value>movie</value>
- <value>music</value>
- </list>
- </constructor-arg>
-
- <constructor-arg>
- <set>
- <value>Lady is GaGa</value>
- <value>GaGa is Lady</value>
- </set>
- </constructor-arg>
-
- <constructor-arg>
- <map>
- <entry key="liuhua" value="man"></entry>
- <entry key="xujinglei" value="female"></entry>
- </map>
- </constructor-arg>
-
- <constructor-arg index="5" value="0">
- </constructor-arg>
- </bean>
<bean id="man" class="com.spring.test.man.Man"> <constructor-arg value="zzy" index="0" > </constructor-arg> <constructor-arg value="10" index="1"> </constructor-arg> <constructor-arg> <list> <value>movie</value> <value>music</value> </list> </constructor-arg> <constructor-arg> <set> <value>Lady is GaGa</value> <value>GaGa is Lady</value> </set> </constructor-arg> <constructor-arg> <map> <entry key="liuhua" value="man"></entry> <entry key="xujinglei" value="female"></entry> </map> </constructor-arg> <constructor-arg index="5" value="0"> </constructor-arg></bean>
最後一個參數ifMarried是一個boolean類型的參數,在配置的時候可以是true/false或者0/1;
二:property的簡單使用
java代碼:
Java代碼
- public class Doctor {
-
- private String name;
- private String sex;
-
-
- public String getName() {
- return name;
- }
-
-
- public void setName(String name) {
- this.name = name;
- }
-
-
-
- public String getSex() {
- return sex;
- }
-
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
-
- public void init(){
- System.out.println("88888888888");
- }
- public void init(String name,String sex){
- this.name = name;
- this.sex = sex;
- }
- }
public class Doctor {private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public void init(){System.out.println("88888888888");}public void init(String name,String sex){this.name = name;this.sex = sex;}}
xml設定檔: Java代碼
- <bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">
- <property name="name" value="doctor"></property>
- <property name="sex" value="i don't know"></property>
- lt;/bean>