三種注入方式
第一種: 基於構造函數
hi.java (bean)
package test_one; public class hi { private String name; public hi(String name) { this.name=name; } public void sayhi() { System.out.println("你好呀 "+this.name); } }
<bean id="binbin" class="test_one.hi" scope="singleton"> <constructor-arg name="name" value="斌斌"/> </bean>

上面是通過配置文件注入字符串
我們來看看如何注入bean
<bean id="binbin" class="test_one.hi" scope="singleton"> <constructor-arg ref="No"/> </bean> <bean id="No" class="test_one.no"></bean>
<!--通過構造函數,將id="No"的bean 注入到 hi 中

總結:
constructor-arg 元素表示通過構造函數注入
屬性: index: 對應構造函數參數列表的位置
type:對應構造函數列表里的的數據類型
name:對應在構造函數列表里的變量名
value: 賦值
ref:對應bean類型的id名
一般都是通過name="" value="" 去注入
而注入bean只要 ref="id名"
其他用法:
<constructor-arg type="java.lang.String" value="45"/>
<constructor-arg index="0" value="20"/>
值得注意的是通過參數名稱來匹配的方法,代碼必須啟用了調試標記編譯,這樣spring才可以從構造函數中查找參數名稱
開發者也可以使用@ConstructorProperties注解來顯式聲明構造函數的名稱
public class ExampleBean{
/***省略字段**/
@ConstructorProperties({"years","ultimateAnswer"})
public ExampleBean(int years,String ultimateAnswer){
this.years=years;
this.ultimateAnswer=ultimateAnswer;
}
}
注意: spring的便利之處就是,將value屬性的字符串類型自動轉換為指定類型
第二種: 基於set方法
hi.java (bean)
package test_one; public class hi { private String name; private int age; private no a; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setA(no a) { this.a = a; } public void print() { System.out.print(name+" "+age+" "+a); } }
<bean id="binbin" class="test_one.hi" scope="singleton"> <property name="name" value="斌斌"/> <property name="age" value="17"/> <property name="a" ref="No"/> </bean> <bean id="No" class="test_one.no"></bean>

總結:
property 表示基於set方法注入
屬性: name: 對應變量名匹配
ref: 如果注入的是bean,對應bean的id名
value: 賦值,spring會自動將字符串類型轉換為對應類型
注意:如果注入的是bean,那么對於property來說就要加上name
<property name="a" ref="No"/>
第三種: 使用p命名空間注入
本質還是基於set注入,不過xml的配置更加簡單,主要一個命名空間就可以對所有的bean賦值,不用多余的嵌套property
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--使用無參構造函數實例化,使用p命令空間注入--> <bean id="binbin" class="test_one.hi" scope="singleton" p:name="斌斌" p:age="17" p:a-ref="No"></bean> <bean id="No" class="test_one.no" p:name="明明"></bean>
第四種:使用c命名空間注入
與p命名空間注入一樣,不過是基於構造函數注入
xmlns:c="http://www.springframework.org/schema/c"
<bean id="C" class="test_one.c_name" c:name="i'm c"/>
如果注入的是數組,List , Set , map, Properties
hi.java (bean)
package test_one; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class hi { private String[] myStrs; private List<String> myList; private Set<Integer> mySet; private Map<String,String> myMap; private Properties myProps; public void setMyStrs(String[] myStrs) { this.myStrs = myStrs; } public void setMyList(List<String> myList) { this.myList = myList; } public void setMySet(Set<Integer> mySet) { this.mySet = mySet; } public void setMyMap(Map<String, String> myMap) { this.myMap = myMap; } public void setMyProps(Properties myProps) { this.myProps = myProps; } public String[] getMyStrs() { return myStrs; } public List<String> getMyList() { return myList; } public Set<Integer> getMySet() { return mySet; } public Map<String, String> getMyMap() { return myMap; } public Properties getMyProps() { return myProps; } public void print() { System.out.println("array:"+myStrs[0]+" "+myStrs[1]+"\nList:"+myList+"\nSet:"+mySet+"\nMap:"+myMap+"\nProperties:"+myProps); } }
<!--使用無參構造函數實例化--> <bean id="binbin" class="test_one.hi" scope="singleton"> <!--給數組注入數據--> <property name="myStrs"> <array> <value>array_one</value> <value>array_two</value> </array> </property> <!--給List注入數據--> <property name="myList"> <array> <value>List_one</value> <value>List_two</value> </array> </property> <!--給set集合注入數據--> <property name="mySet"> <list> <value>1314</value> <value>520</value> </list> </property> <!--注入map數據--> <property name="myMap"> <props> <prop key="testA">map_one</prop> <prop key="testB">map_two</prop> </props> </property> <!--注入Properties數據--> <property name="myProps"> <map> <entry key="testA" value="properties_one"/> <entry key="testB"> <value>properties_two</value> </entry> </map> </property> </bean>
可以發現我將array, list,set 的元素混用了,將map和props混用了
也就是說 array,list ,set之間可以混用,map與props之間可以混用。 不過在實際開發時還是元素和數據類型對應使用
如果集合的數據類型是其他類型,在配置文件中,會自動將value的值轉化為對應類型
注入Properties數據的另一種方式,更加簡便
key=value 的寫法
<!--注入Properties數據--> <property name="myProps"> <value> testA=Properties_one testB=Properties_two </value> </property>
引用bean注入
參考:https://www.cnblogs.com/CloudComputing-binbin/p/15939822.html
內部bean注入
內部bean也稱為內部匿名bean, 如果需要注入bean並且只用於一個特定的屬性,建議使用內部bean
內部bean無法被外部bean引用,哪怕你加上id,她會無視scope標簽

<bean id="binbin" class="test_one.hi" scope="singleton"> <!--給數組注入數據--> <property name="myStrs"> <array> <bean class="test_one.no" parent="parent"/> </array> </property> </bean>
那個內部bean是通過抽象bean注入的,想了解抽象bean請參考:三種實例化bean以及非實例化抽象bean
Null以及空字符的值
<bean class="ExampleBean"> <property name="email" value=""/> </bean>
什么示例與下列java代碼效果一樣
ExampleBean.setEmail("");
<null/>元素表示null(空指針)
<bean class="ExampleBean"> <property name="email"> <null/> </property> </bean>
與下面的java代碼效果一樣
ExampleBean.setEmail(null);
