據我所學,spring實現依賴注入(DI)的方式分為三大類:基於構造器(構造方法)的依賴注入、基於setter的依賴注入、其他方式(c命名空間、p命名空間等)。其中推薦使用setter方法注入,這種注入方式也是最多人使用的。
下面我們通過代碼來舉例三種注入方式:
1.基於構造器(構造方法)的依賴注入
這種方式是通過實體類中的構造方法來完成屬性的賦值,所以實體類中必須含有帶參的構造方法!
首先先編寫實體類User,如下:
public class User { private int id; private String name; private int age; private String pwd; public User() { } public User(int id, String name, int age, String pwd) { this.id = id; this.name = name; this.age = age; this.pwd = pwd; System.out.println("User的全參構造"); } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", pwd='" + pwd + '\'' + '}'; } }
然后我們就可以編寫配置文件了,一共有三種方式,這三種方式都是一樣的,掌握一種便可以了,推薦使用方式一。
方式一:通過屬性名稱賦值
<bean id="user" class="com.kuang.pojo.User"> <constructor-arg name="id" value="0000"/> <constructor-arg name="age" value="21"/> <constructor-arg name="pwd" value="123456"/> <constructor-arg name="name" value="張三"/> </bean>
方式二:通過下標賦值
注意:這里index就是實體類中屬性的索引,從0開始,實體類中從上到下第一個屬性的索引為0,第二個屬性的索引為1....以此類推。
<!--通過下標賦值--> <bean id="user" class="com.kuang.pojo.User"> <constructor-arg index="0" value="0000"/> <constructor-arg index="1" value="張三"/> <constructor-arg index="2" value="21"/> <constructor-arg index="3" value="123456"/> </bean>
方式三:通過類型賦值
<bean id="user" class="com.kuang.pojo.User"> <constructor-arg type="int" value="0000"/> <constructor-arg type="java.lang.String" value="張三"/> <constructor-arg type="int" value="21"/> <constructor-arg type="java.lang.String" value="123456"/> </bean>
2.基於setter的依賴注入
這種方式是通過實體類中的setter方法來完成屬性的賦值的,所以實體類中必須有各個屬性的setter方法!
Student實體類如下:
public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> game; private String wife; private Properties info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbys() { return hobbys; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGame() { return game; } public void setGame(Set<String> game) { this.game = game; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", card=" + card + ", game=" + game + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
Address實體類如下:
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
配置文件的代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="Address" class="com.kuang.pojo.Address">
<property name="address" value="北京"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="張三"/> <property name="address" ref="Address" /> <property name="books"> <array> <value>紅樓夢</value> <value>西游記</value> <value>水滸傳</value> <value>三國演義</value> </array> </property> <property name="hobbys"> <list> <value>游泳</value> <value>跑步</value> <value>爬山</value> </list> </property> <property name="card"> <map> <entry key="身份證" value="123456789987654"/> <entry key="銀行卡" value="654873264654879"/> </map> </property> <property name="game"> <set > <value>英雄聯盟</value> <value>王者榮耀</value> <value>穿越火線</value> </set> </property> <property name="wife" > <null/> </property> <property name="info"> <props> <prop key="學號">1665487956</prop> <prop key="性別">男</prop> <prop key="姓名">小明</prop> </props> </property> </bean> </beans>
3.其他方式(c命名空間、p命名空間等)
