3 依赖注入(DI)
3.1 Setter 注入
Setter 方法注入即通过 Setter 方法为对象的属性注入值,是最常用的方式。
下面使用 Setter 注入来介绍不同类型的变量的注入方式。
首先书写测试的用例:
实体类:
package sharm.temp;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package sharm.temp;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public Student() {
}
public Student(String name, Address address, String[] books, List<String> hobbys, Map<String, String> card, Set<String> games, String wife, Properties info) {
this.name = name;
this.address = address;
this.books = books;
this.hobbys = hobbys;
this.card = card;
this.games = games;
this.wife = wife;
this.info = 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> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
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;
}
public void show(){
System.out.println("name="+ name
+ ",address="+ address.getAddress()
+ ",books="
);
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println("\n爱好:"+hobbys);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("wife:"+wife);
System.out.println("info:"+info);
}
}
1)常量注入
<bean id="address" class="sharm.temp.Address">
<property name="address" value="LanXi"/>
</bean>
2)Bean 注入
<bean id="Student" class="sharm.temp.Student">
<property name="name" value="Sharm"/>
<!--地址是一个引用,引用另一个 bean-->
<property name="address" ref="address"/>
</bean>
3)数组注入
<bean id="Student" class="sharm.temp.Student">
<property name="books">
<array>
<value>三体</value>
<value>人生</value>
<value>活着</value>
</array>
</property>
</bean>
4)List 注入
<bean id="Student" class="sharm.temp.Student">
<property name="hobbys">
<list>
<value>跑步</value>
<value>健身</value>
<value>篮球</value>
</list>
</property>
</bean>
5)Set 注入
<bean id="Student" class="sharm.temp.Student">
<property name="games">
<set>
<value>学习</value>
<value>学习+1</value>
</set>
</property>
</bean>
6)Map 注入
<bean id="Student" class="sharm.temp.Student">
<property name="card">
<map>
<entry key="中国银行" value="12323344"/>
<entry key="中国农业银行" value="23344444"/>
</map>
</property>
</bean>
7)Null 注入
<bean id="Student" class="sharm.temp.Student">
<property name="wife">
<null/>
</property>
</bean>
8)配置文件注入
<bean id="Student" class="sharm.temp.Student">
<!--prop 标签对应的是配置文件-->
<property name="info">
<props>
<prop key="联系方式">share_me@126.com</prop>
<prop key="网名">是志明啊</prop>
</props>
</property>
</bean>
3.2 构造器注入
即通过构造器初始化的过程为对象的属性注入值。
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public Student(String name, Address address, String[] books, List<String> hobbys, Map<String, String> card, Set<String> games, String wife, Properties info) {
this.name = name;
this.address = address;
this.books = books;
this.hobbys = hobbys;
this.card = card;
this.games = games;
this.wife = wife;
this.info = info;
}
}
3.3 其它方式注入
3.3.1 p 命名空间注入
p 命名空间注入 ,即属性命名空间注入。实体类的属性必须要设置 setter 方法。
<!--要加上下面这一句,告诉 Spring 我们需要用 p 命名空间注入-->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@somewhere.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@somewhere.com"/>
</beans>
3.3.2 c 命名空间注入
c 命名空间注入 ,即构造器(constructor)命名空间注入。实体类的属性不仅要设置 setter 方法,而且还必须要加上有参构造器。
<!--要加上下面这一句,告诉 Spring 我们需要用 c 命名空间注入-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>